Method in Java

Method in java is one kind of function that do some action when it is called. We must declare a method in java before calling or invoking it.

Method declaration: Method declaration involves three things. return type followed by parentheses () method body.

type method_name (){... //code to be execute}

Imagine, inside our Main class file we have a method called printGreeting(){} and it prints a simple "Java is awesome" message.

firstly create a class file called Main.java inside your java_examples application.

It probably contains main method.

So, your main class file should be like following:


public class Main {
    public static void main(String[] args) {
        //code to be executed.
    }
}

Now, Inside the main class file create a method called printGreeting(){} //it print simple greeting message.

public class Main {

// creating a printGreeting() method 
public static void printGreeting(){
system.out.println("Java is Awesome!")
}
   public static void main(String[] args) {
    
//call the printGreeting() method inside main method
printGreeting();

   }

}
/**
*output:
* Java is Awesome!
*
*/

You can call a method multiple times.

Like:


public class Main {

// creating a printGreeting() method 
public static void printGreeting(){
system.out.println("Java is Awesome!")
}
   public static void main(String[] args) {
    
//call the printGreeting() method inside main method
printGreeting();
printGreeting();
printGreeting();

   }

}
/**
*output:
* Java is Awesome!
* Java is Awesome!
* Java is Awesome!
*/

# Non static method: A method that needs object to initialize or execute it's behaviors that called non static method or instance method.

Like:


public class Main {

// creating a printGreeting() object method 
public void printGreeting(){
system.out.println("Java is Awesome!")
}
   public static void main(String[] args) {
    
//creating an object of Main 
Main obj = new Main();

//call the printGreeting()
obj.printGreeting();

   }

}
/**
*output:
* Java is Awesome!
*
*/

You see, how we have created an object of Main class file and the invoke the printGreeting() method.

# Method may have return type as well.

You see in the method declaration we have used void keyword. It means, the method doesn't return any value. But, what if we want a method to return any value?

Then, instead of using void, we have to use int, String, float or even valid object type to return something.

See another example:


public class Main {

// creating a printGreeting() method that returns int value
public static int printSummation(){
         return 10+10;
}
   public static void main(String[] args) {
    
//call the printSummation () method inside main method
System.out.println(printSummation());

   }

}
/**
*output:
* 20
*
*/

Now, the method returns int value.

Let's say you want the method should return String value.

Just write the method like this:


public static String printGreeting(){
return "Java is Awesome!";
}

And then call the method as usual.

# Method that takes parameters

In java, a method may contain parameters as well. But the parameters are recognized local variables and they must have a type and name. Any valid statement can be parameters in Java.

# Creating a method that contains parameters.

Guess, we have a method called printAddition and it prints the summation of two int local variables.

Look at the example below:


public class Main {

// creating a printGreeting() method that returns int value
public static int printAddition(int x, int y){
         return x+y;
}
   public static void main(String[] args) {
    
//call the printSummation () method inside main method
System.out.println(printSummation(10, 20));

   }

}
/**
*output:
* 30
*
*/

You may notice that, when we invoke the method we have passsd the parameters value. They must match the type and number as well.

Remember, what local variables we have used inside the parentheses at the time of method declaration are called parameters.

And, the value we have used or passed at the time of method invocation are called arguments. The parameter numbers and types should be match with arguments.

This is an ongoing article. Will be improved..