Java Methods

Java Methods

A method defines the behavior of an object.

The methods consist of modifiers, declaring a name for the method, data type that the method returns, parameters and a block of instructions that are executed when the method is executed.

Modifiers can be:
Access specifier: public, protected, private.
One of the reserved keywords: static, abstract, final, native, synchronized.

Method example

public class MethodExample{  
  void m1(int x,int y){
      System.out.println(x+y);
    }  
  
  void m2(String name){
      System.out.println("Name: "+name);
    }      
  public static void main(String args[]){
      MethodExample ob = new MethodExample(); 
      ob.m1(10,20);  
      ob.m2("Java");
  
  }  
}