Methods in Java

If you have learnt c/c++ before, then just understand methods are exactly a functions

In Java, a method is function which is used to expose the behavior of an object.

Inside methods, we have codes which only runs if they are called. Also we can pass data into a method which is also called parameters.

We should always use method if we are about to re-use that code. It saves from code repetition.
Syntax:

<ReturnType> <methodName> () {
    statements
}
Here, ReturnType is Data Type of what our method will return value after executing. We can use void if we dont want data in return. Also we can use static keyword at very beginning, that means a method can be accessible directly without making object.

We will learn about object in next chapter so we will be using static keyword here but remember thats optional. We only use static keyword if we have to access that method without creating object.

Creating method

Lets create a simple method inside our class.
Code:

public class LearnMethod { void Display() { System.out.println("I am from display."); } }


Calling method

Lets try to call that method. We must have at least one main method in our program. main method is only method compiler searches for. We can call other methods in our main method to execute them.
Code:

public class LearnMethod { public static void main (String[] args) { //calling another method Display(); } static void Display() { System.out.println("I am from display."); } }
Don't worry if you don't understand public keyword we've used. We will talk about it later.
Output:
I am from display.


Passing value

Methods are not always good to work on their own. Sometimes they may also need some values to work. Lets see another example, where will pass value in method.
Code:

public class LearnMethod { public static void main (String[] args) { Add(4,6); } static void Add(int x, int y) { int sum = x + y; System.out.println("Total: " + sum); } }
In above example, you can see that to pass the value, we can simply attach value while calling method and to receive value, we must have data type and variable defined inside parenthesis next to method.
Output:
Total: 10


Also we can pass reference of any value. Take a look at this example, where user inputs age and method decides if he is eligible to vote or not.
Code:

import java.util.Scanner; public class LearnMethod { public static void main (String[] args) { Scanner inp = new Scanner(System.in); System.out.println("Enter Your Age: "); int age = inp.nextInt(); check(age); } static void check(int age) { if(age >= 18) { System.out.println("You can vote."); } else { System.out.println("You cannot vote."); } } }
In above example, you can see that we only passed reference and all operations are done in another method.
Output:
Enter Your Age:
20
You can vote.


Returning value

Sometimes we have to get return value from method. Lets see another example, where we will return value to our main method.
Code:

public class LearnMethod { public static void main (String[] args) { int sum = add(4,7); int diff = sub(9,4); System.out.println("Sum: " + sum); System.out.println("Diff: " + diff); } static int add(int x, int y) { int z = x + y; return z; } static int sub(int x, int y) { int z = x - y; return z; } }
Here we have two methods for addition and subtraction. Here, we passed the value to that method and took out the result. And we stored the value in our own variable in main method. Remember that return keyword is used to return values from method.
Output:
Sum: 11
Diff: 4


Class in Java Previous Next Access Modifiers in Java