Method arguments and return values in java

Overview

In this tutorial, you are going to learn about method arguments and return values in java. We will cover the following topics

  1. What is a Method
  2. Types of methods in Java
  3. Understanding method arguments
  4. Method return values
  5. Calling a method with parameters

What is a method?

Java is an object oriented programming language. That means we can model read-world entities. These entities have “behavior”. Behavior determines how an instance of that class works. Here are some behaviors that a Vehicle class might have:

  • Start engine
  • Stop engine
  • Change gear

We can model the Vehicle in Java by creating a class Vehicle.

public class Vehicle {
  //attributes

  // Begin method definations
  public void startEngine(){}
  public void stopEngine(){}
  public void changeGear(){}

}

In order to define an object’s behavior, you create methods, a set of reusable Java statements that accomplish a specific task. Methods look and behave just like functions in other programming languages such as Python, Perl, PHP, but are defined and accessible solely inside a class. Java does not have functions defined outside classes (as C++ does).

Types of methods in Java

Java has two types of methods: Static and Instance Methods

Static Methods

A static keyword on a method definition means that the method can be accessed directly, without creating a class instance. Using the Vehicle class example, let’s modify startEngine so we can access it directly, without creating an object of Vehicle

public class Vehicle {
     //attributes

     // Begin method definitions
     public static void startEngine(){}
   }

To access the startEngine method, we simply use the notation ClassName.MethodName. In our case, we use Vehicle.startEngine()

Instance Methods

Unlike static methods, instance methods are accessible through the class object. This means that you first have to create an object of a class, then you call the method on that object.

Vehicle vehicleObject = new Vehicle(); // Create the object
vehicleObject.startEngine(); // Through the object, call the method

Understanding method arguments

When a method is invoked, the arguments are the data you pass into the methods’ parameters. We have an article that explains the difference between method arguments and parameters.

Let’s modify our changeGear method by adding an integer parameter. This parameter will represent the gear the Vehicle will change to.

public class Vehicle {
  //attributes
  private int gear = 2;

  // Begin method definitions
  public void changeGear(int gear){
    this.gear = gear;
  }
}

Let’s now use the changeGear method to change the gears of the Vehicle. We’ll change gears from 1 to 3.

Vehicle vehicleObject = new Vehicle(); // create Vehicle object
vehicleObject.changeGear(1); // argument is 1
vehicleObject.changeGear(2); // argument is 2
vehicleObject.changeGear(3); // argument is 3

Method return values

So far, the methods in the Vehicle class do not return any value. Any method declared void doesn’t return a value.

A method’s return type is declared in its method declaration. Within the body of the method, you use the return statement to return the value. The return value’s data type must match the method’s declared return type. For example, you can not return a String when your method-declared return type is integer.

Let’s modify the Vehicle class to include a method getGear. The method will return the gear at which the Vehicle is moving at. As you might have guessed, the return type of this method is int.

public class Vehicle {
  //attributes
  private int gear;

  // Begin method definition
  public int getGear(){
    return gear;
  }
}

And here is how to get the return value from the method

Vehicle vehicleObject = new Vehicle(); // create Vehicle object
int gear = vehicleObject.getGear(); // return value is stored in variable 'gear'
System.out.println("The vehicle is in gear: " + gear);

The output:

The vehicle is in gear: 2

The return value should be stored in a variable. In our example above, we stored the returned value into a new variable gear.

Conclusion

We have covered method arguments and return values in java. To use methods in Java, you need to define them inside a class. Methods can either be static or instance methods. Static methods do not require an instance of a class to be accessed.

We hope you have enjoyed this article. Please share with friends!

Share this article

About Michael Mulikita

Founder of Simplecodegeeks.com, I love programming and open source technologies. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

View all posts by Michael Mulikita →

Leave a Reply