Non-Static Methods
Non-static methods in Java are fairly similar to the static methods we already know about. The difference is that non-static methods are associated with an object of a given class type, not with the class itself. This means that we can call a method for each of our object variables, and that the method will use that object’s fields in its calculations. (This is not always true, but we’ll pretend for now.) Here’s the format of a method:
visibility returnType name(args)
{
//code
//possible return statement
}Let’s discuss each part separately. The visibility can be either public or private,
depending on whether we want to call the method outside this class. (It can later be
protected as well.) The returnType specifies what type of value will be returned. If the
method doesn’t return anything, its return type is void. name is just the name of the method,
which you will use when calling it. args are optional arguments (parameters) for the
method.
If a method has a non-void return type, it must have a return statement. This looks like:
return value/variable;where that value or variable has the same type as returnType.
Here’s a very simple method that just prints “Hello, World!” to the screen:
public void greeting()
{
System.out.println("Hello, World!");
}Method arguments
Method arguments (parameters) are just values passed to the method. Each method can have zero or many arguments. Here’s what the argument list should look like:
type1 name1, type2 name2, ...If a method has no arguments, you still need to include the () after the method name (like the
greeting method above). Here’s an example of a method that takes two ints, and prints their
product to the screen:
public void printProduct(int num1, int num2)
{
int product = num1*num2;
System.out.printf("The product is %d%n", product);
}Returning a value
So far, we’ve only looked at methods that have a void return type – that don’t return anything.
Here’s a method that returns something:
public int product (int num1, int num2)
{
int product = num1*num2;
return product;
}This method has an int return type, and so it includes a return statement. The variable
returned (product) also has type int.
Calling non-static methods from the same class
Now that we can write methods, we need to be able to call them. This will be different depending on whether we’re calling a method that’s in the same class as us, or in a different class. Here’s how we call a method in the same class as us:
methodName(args)Here, methodName is the name of the method we’re calling, and args are the values of the
parameters we’re passing. The parameters must have the same type as the corresponding
arguments in the methods definition. Here’s an example of calling the printProduct method
(from the same class):
int val1 = 5;
int val2 = 7;
printProduct(val1, val2);The VALUES of val1 and val2 (5 and 7) are passed to printProduct, and stored in the
arguments num1 and num2.
When the method we’re calling returns a value, we need to store that return value when we call
it. Here’s an example of calling the product method (from the same class):
int val1 = 5;
int val2 = 7;
int result = product(val1, val2);Now the product of val1 and val2 (35) is stored in the result variable.
Notice that this syntax for calling methods is so far similar to what we’ve done to call static methods.
Calling non-static methods from a different class
Calling non-static methods from a different class works similarly, expect we first need an object of that class type. Let’s look at an example class:
public class Rectangle
{
private int length;
private int width;
public Rectangle(int l, int w)
{
length = l;
width = w;
}
public int area()
{
return length*width;
}
public int perimeter()
{
return length*2 + width*2;
}
}Before we can call any of the methods in Rectangle, we need to create a new Rectangle
object. Let’s create two – a 4x6 rectangle and a 3x5 rectangle:
Rectangle r1 = new Rectangle(4, 6);
Rectangle r2 = new Rectangle(3, 5);Now, here’s the format for calling a non-static method in a different class:
objectName.methodName(args);So, to call the area method on the 4x6 rectangle, we’d do:
r1.area();However, the area method returns a value, so we probably want to store or do something with a
result. We’ll store the area of the 4x6 rectangle, and print the perimeter of the 3x5 rectangle:
int result = r1.area();
System.out.printf("The perimeter is %d%n", r2.perimeter());Method overloading
Method overloading is when you have two or more versions of the same method, and each version has a different argument list. For example, suppose you wanted to define a method that computed the max of two numbers. You might want to do this for both ints and doubles. Here’s how:
public int max(int num1, int num2)
{
if (num1 > num2) return num1;
else return num2;
}
public double max(double num1, double num2)
{
if (num1 > num2) return num1;
else return num2;
}Notice that the two versions of max have the same name, but different argument lists. They also
have different return types, but overloaded methods do not have to work this way. The compiler
can figure out which one you want to call based on what types you pass. For example:
int max1 = max(4, 7)would call the first version of max since the arguments are both ints. On the other hand:
double max2 = max(5.4, 8.76);would call the second version since the arguments are both doubles.
Constructors can also be overloaded. This is done by creating two constructors (both with the name of the class) with different argument lists.