×
>
<

Aptitude

Method in Java

The method in Java is a collection of instructions that performs a specific task. It provides the reusability of code.

A method is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation. It is used to achieve the reusability of code.

We write a method once and use it many times. We do not require to write code again and again. It also provides the easy modification and readability of code, just by adding or removing a chunk of code.

In Java most important method is the main() method !

Method Declaration

The method declaration provides information about method attributes, such as visibility, return-type, name, and arguments.

  • Method Name: Method name is its unique identity which differs it from other with association with the parameters.
  • Access Specifier: Access specifier or modifier is the access type of the method. It specifies the visibility of the method. Java provides four types of access specifier:
    1. Public: The method is accessible by all classes when we use public specifier in our application.
    2. Private: When we use a private access specifier, the method is accessible only in the classes in which it is defined.
    3. Protected: When we use protected access specifier, the method is accessible within the same package or subclasses in a different package.
    4. Default: When we do not use any access specifier in the method declaration, Java uses default access specifier by default. It is visible only from the same package only.
  • Return Type: Return type is a data type that the method returns. It may have a primitive data type, object, collection, void, etc. If the method does not return anything, we use void keyword.
  • Method Name: It is a unique name that is used to define the name of a method. It must be corresponding to the functionality of the method. Suppose, if we are creating a method for subtraction of two numbers, the method name must be subtraction(). A method is invoked by its name.
  • Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It contains the data type and variable name. If the method has no parameter, left the parentheses blank.
  • Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed within the pair of curly braces.
Method Types

There are two types of methods in Java:

  • Predefined Methods
  • User Defined Methods

Predefined Method

In Java, predefined methods are the method that is already defined in the Java class libraries is known as predefined methods. It is also known as the standard library method or built-in method. We can directly use these methods just by calling them in the program at any point. Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc.

Let's see an example of the predefined method.
  
public class FineMax   
{  
    public static void main(String[] args)   
    {  
        // using the max() method of Math class  
        System.out.print("The greatest among these number is: " + Math.max(9,10));  
    }  
}
  

Output :

The greatest among these number is: 10

User-defined Method

The method which do not have any predefined library known as User-defined methods. As programmer has to make and complete the requried algorithms.

Let's see an example of the User-defined method.
  
public class Main   
{  
    public static void main(String[] args)   
    {  
        findEvenOdd(2);
        
    }
    
    //user defined method  
    public static void findEvenOdd(int num)  
        {  
        //method body  
        if(num%2==0)   
            System.out.println(num+" is even");   
        else   
            System.out.println(num+" is odd");  
        }  
    }
  

Output :

num is even
Static and Non static Method

Static Method

A method that has static keyword is known as static method. In other words, a method that belongs to a class rather than an instance of a class is known as a static method. We can also create a static method by using the keyword static before the method name.

The main advantage of a static method is that we can call it without creating an object. It can access static data members and also change the value of it. It is used to create an instance method. It is invoked by using the class name. The best example of a static method is the main() method.

Let's see an example of the static method.
  
public class Main  
{  
    public static void main(String[] args)   
    {  
        show();  
    }  
    static void show()   
    {  
        System.out.println("I am inside the static method !");  
    }  
} 
  

Output :

I am inside the static method !

Non Static Method or Instance Method

It is a non-static method defined in the class. Before calling or invoking the instance method, it is necessary to create an object of its class.

As to invoke this method we need to create an oobject i.e. instance of the class, so it is also called as instance method.

Let's see an example of an instance method.
  
public class InstanceMethodExample  
{  
    int s;
    
    public static void main(String [] args)  
    {  
        //Creating an object of the class  
        InstanceMethodExample obj = new InstanceMethodExample();  
        
        //invoking instance method   
        System.out.println("The sum is: "+obj.add(5, 8);  
    }  
          
        
    //instance method 
    public int add(int a, int b)  
    {  
        s = a+b;
        
        //returning the sum  
        return s;  
    }  
}
  

Output :

The sum is: 8