Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.7. Java method calling

3.7. Java method calling
Last updated: 23 January 2013.

In the previous tutorials, you have seen simple method calls. So far, the methods you have seen in action did not have any arguments and did not return any value. This tutorial will show you how to pass arguments to a method and how to make a method return a value.

Let's take a look at the method invert shown in the tutorial Arithmetic operators examples (very last example). Here is the code:

public class JavaOperators {

   
public void invert(){

         
boolean bool = true;

          System.out.println
(! bool);
   
}

   
// The other methods of the class JavaOperators...
    //...
    //...
}

The method invert declares and initializes a local variable of type boolean and prints its negation to the standard output. In order to call the method, I created a class named App containing a main method as shown in the sample below:

public class App {

   
public static void main(String[] args) {

         
new JavaOperators().invert();

   
}

}

The main method of the class App is the caller and the method invert is the callee. As you can see, if you run the program, you always get the same literal printed to the standard output (false) since the boolean that is inverted is declared and initialized locally within the method invert.

The whole thing would be more flexible if the caller could tell the callee which value to invert (true or false), so that the caller can for example invert true and false successively and get the right outputs. To that end, the method invert must have a boolean argument as shown below (arguments are also called parameters):

public class JavaOperators {

   
public void invert(boolean value){

         
value = ! value;

          System.out.println
(value);
   
}

   
// The other methods of the class JavaOperators...
    //...
    //...
}

The signature of the method invert has changed. It now has an argument of type boolean named value. Typically, an argument declaration is similar to a variable declaration. If there is more than one argument, they must be separated by commas. For example, the signature of a method named getSurface which calculates the surface of a rectangle would look something like this:

public void getSurface (int width, int height);

But let's get back to the subject. The main method of the class App can now pass to the method invert the value that is to be inverted:

public class App {

   
public static void main(String[] args) {

         
boolean bool = false;

         
new JavaOperators().invert(bool);

   
}

}

It is important to know that in the Java language, the arguments are passed by value during method calls. In the example shown above, the argument named bool in the main method of the class App is passed by value, which means that a copy of the value of bool is passed to the method invert. That copy is changed inside the method invert but the value of bool remains unchanged. Let's print the value of bool prior and after the call to the method invert:

public class App {

   
public static void main(String[] args) {

         
boolean bool = false;

          System.out.println
(bool);

         
new JavaOperators().invert(bool);

          System.out.println
(bool);

   
}

}

The output is:

false
true
false

Instead of just printing the negation of its boolean argument to the standard output, the method invert can return the negation to the caller as shown here:

public class JavaOperators {

   
public boolean invert(boolean value){

         
value = ! value;

         
return value;
   
}

   
// The other methods of the class JavaOperators...
    //...
    //...
}

In the code above, the return type of the method invert is not void anymore but boolean, which declares that the method invert now returns a boolean to the caller. The keyword return exits the method invert and returns the boolean value to the caller that can store the returned boolean in a variable named result, as shown below:

public class App {

   
public static void main(String[] args) {

         
boolean bool = false;

         
boolean result = new JavaOperators().invert(bool);

          System.out.println
(result);

   
}

}

The output is:

true

Now let's add to the class JavaOperators another method invert that returns the inverted boolean to the caller and also prints the passed character to the standard output:

public boolean invert(boolean value, char printMe){

   
System.out.println(printMe);

    value = ! value;

   
return value;

}

The method invert shown above does not conflict with the method invert which only has one boolean argument since each method has a unique signature. The Java language allows you to have several methods that have the same name in the same class, provided that they have different signatures:

public class JavaOperators {


   
public boolean invert(boolean value){

         
value = ! value;

         
return value;

   
}


   
public boolean invert(boolean value, char printMe){

         
System.out.println(printMe);

          value = ! value;

         
return value;

   
}

   
// The other methods of the class JavaOperators...
    //...
    //...

}

The two methods shown above are termed overloaded methods. They have the same name but they differ in their signatures. Let's modify the class App to call the second method invert:

public class App {

   
public static void main(String[] args) {

         
boolean bool = false;

         
boolean result = new JavaOperators().invert(bool, 'c');

          System.out.println
(result);

   
}

}

The output is:

c
true


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.7. Java method calling
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.8. Java arrays

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7