Java Programming/Methods

Methods are how we communicate with objects. When we invoke or call a method we are asking the object to carry out a task. We can say methods implement the behaviour of objects. For each method we need to give a name, we need to define its input parameters and we need to define its return type. We also need to set its visibility (private, protected or public). If the method throws a checked exception, that needs to be declared as well. It is called a method definition. The syntax of method definition is: We can declare that the method does not return anything using the Java keyword. For example:

When the method returns nothing, the keyword at the end of the method is optional. When the execution flow reaches the keyword, the method execution is stopped and the execution flow returns to the caller method. The keyword can be used anywhere in the method as long as there is a way to execute the instructions below:

In the code section 3.68, the keyword at line 5 is well placed because the instructions below can be reached when   is negative or equal to 0. However, the keyword at line 8 is badly placed because the instructions below can't be reached.

Question 3.9: Consider the following code:

The code above will return a compiler error. Why?

The method is supposed to return a but when   is negative or equal to 0, it returns nothing.

Parameter passing
We can pass any primitive data types or reference data type to a method.

Primitive type parameter
The primitive types are passed in by value. It means that as soon as the primitive type is passed in, there is no more link between the value inside the method and the source variable:

As you can see in code section 3.70, the  method has not modified the value of.

Reference type parameter
The object references are passed by value. It means that:
 * There is no more link between the reference inside the method and the source reference,
 * The source object itself and the object itself inside the method are still the same.

You must understand the difference between the reference of an object and the object itself. An object reference is the link between a variable name and an instance of object:

An object reference is a pointer, an address to the object instance.

The object itself is the value of its attributes inside the object instance:

Take a look at the example above:

The name has changed because the method has changed the object itself and not the reference. Now take a look at the other example:

The name has not changed because the method has changed the reference and not the object itself. The behavior is the same as if the method was in-lined and the parameters were assigned to new variable names:

Variable argument list
Java SE 5.0 added syntactic support for methods with variable argument list, which simplifies the typesafe usage of methods requiring a variable number of arguments. Less formally, these parameters are called varargs. The type of a variable parameter must be followed with, and Java will box all the arguments into an array:

When calling the method, a programmer can simply separate the points by commas, without having to explicitly create an array of  objects. Within the method, the points can be referenced as,  , etc. If no points are passed, the array has a length of zero.

A method can have both normal parameters and a variable parameter but the variable parameter must always be the last parameter. For instance, if the programmer is required to use a minimum number of parameters, those parameters can be specified before the variable argument:

Return parameter
A method may return a value (which can be a primitive type or an object reference). If the method does not return a value we use the Java keyword.

However, a method can return only one value so what if you want to return more than one value from a method? You can pass in an object reference to the method, and let the method modify the object properties so the modified values can be considered as an output value from the method. You can also create an Object array inside the method, assign the return values and return the array to the caller. However, this gives a problem if you want to mix primitive data types and object references as the output values from the method.

There is a better approach, define a special return object with the needed return values. Create that object inside the method, assign the values and return the reference to this object. This special object is "bound" to this method and used only for returning values, so do not use a public class. The best way is to use a nested class, see example below:

In the above example the  method returns an object reference that contains both values of the name and the age. See below how you may use that object:

Question 3.10: Consider the following code:

The code above will return a compiler error. Why?

The method is supposed to return a but at line 4, it returns , which is a String.

Special method, the constructor
The constructor is a special method called automatically when an object is created with the keyword. Constructor does not have a return value and its name is the same as the class name. Each class must have a constructor. If we do not define one, the compiler will create a default so called empty constructor automatically.

Static methods
A static method is a method that can be called without an object instance. It can be called on the class directly. For example, the  method of the   class is a static method:

The static keyword makes attributes instance-agnostic. This means that you cannot reference a static attribute of a single object (because such a specific object attribute doesn't exist). Instead, only one instance of a static attribute exists, whether there is one object in the JVM or one hundred. Here is an example of using a static attribute in a static method:

You can notice that when you use,   is a static attribute of the   class. A static attribute is related to a class, not to any object instance. This is how Java achieves one universal output stream that we can use to print output. Here is a more complex use case:

Question 3.11: Visit the Oracle JavaDoc of the class.

How many static fields does this class have?

4.
 * and
 * and
 * and


 * To learn how to overload and override a method, see Overloading Methods and Constructors.