News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

Java Tutorial – 10 – Methods

Methods are reusable code blocks that will only execute when called.

Defining methods

A method can be created by typing void followed by the method’s name, a set of parentheses and a code block. The void keyword means that the method will not return a value. The naming convention for methods is the same as for variables – a descriptive name with the first word in lowercase and any other words initially capitalized.

class MyApp
{
  void myPrint()
  {
    System.out.print("Hello");
  }
}

Calling methods

The method above will simply print out a text message. To invoke (call) it from the main method an instance of the MyApp class must first be created. The dot operator is then used after the instance’s name in order to access its members, which include the myPrint method.

public static void main(String[] args) 
{
  MyApp m = new MyApp();
  m.myPrint(); // "Hello"
}

Method parameters

The parentheses that follow the method name are used to pass arguments to the method. To do this the corresponding parameters must first be added to the method declaration in the form of a comma separated list.

void myPrint(String s)
{
  System.out.print(s);
}

A method can be defined to take any number of arguments and they can have any data types, just ensure the method is called with the same types and number of arguments.

public static void main(String[] args) 
{
  MyApp m = new MyApp();
  m.myPrint("Hello"); // "Hello"
}

To be precise, parameters appear in method definitions, while arguments appear in method calls. However, the two terms are sometimes used interchangeably.

Return statement

A method can return a value. The void keyword is then replaced with the data type that the method will return, and the return keyword is added to the method body with an argument of the specified return type.

String getPrint()
{
  return "Hello";
}

Return is a jump statement that causes the method to exit and return the specified value to the place where the method was called. For example, the method above can be passed as an argument to the getPrint method since the method evaluates to a string.

public static void main(String[] args) 
{
  MyApp m = new MyApp();
  System.out.print( m.getPrint() ); // "Hello"
}

The return statement may also be used in void methods to exit before the end block is reached.

void myPrint(String s)
{
  System.out.print(s); return;
}

Method overloading

It is possible to declare multiple methods with the same name as long as the parameters vary in type or number. This is called method overloading and can for example be seen in the implementation of the System.out.print method. It is a powerful feature that allows a method to handle a variety of arguments without the programmer needing to be aware of using different methods.

void myPrint(String s)
{
  System.out.print(s);
}
 
void myPrint(int i)
{
  System.out.print(i);
}

Passing arguments

Java is different from many other languages in that all method parameters are passed by value. In fact, they cannot be passed by reference. For value data types (primitive types) this means that only a local copy of the variable is changed within the method, so the change will not affect the original variable. For reference data types (classes, interfaces and arrays) it means that only a copy of the memory address is passed to the method. Therefore, if the entire object is replaced the change will not propagate back to the caller, but changes to the object will affect the original since the copy points to the same memory location.

public static void main(String[] args)
{
  MyApp m = new MyApp();
  int x = 0;              // value data type
  m.set(x);               // value is passed
  System.out.print(x);    // "0"
 
  int[] y = {0};          // reference data type
  m.set(y);               // address is passed
  System.out.print(y[0]); // "10"
} 
 
void set(int a) { a = 10; }
void set(int[] a) { a[0] = 10; }
Recommended additional reading:
Sams - Teach Yourself Java in 24 Hours