Instance Method without parameter

Syntax:

modifier return_type method_name( )
{
        method body ;
}
  • modifier: It defines the access type of the method, and it is optional to use.
  • return_type: Method may return a value. Ex:- int, void, String, char, float, etc.
  • method_name: This is the method name you can write anything as you write the variable name.
  • method body: The method body describes what the method does with statements.

Example:

public void disp( )
{
       int a= 10;
    System.out.println(a);
}

Calling Instance Method:

You can not call an instance method in the static method directly, so the Instance method can be invoked using an object of the class. We know that the java program’s execution starts from the main method and the main method is static, so we can not directly call the instance method. We have to create the class object; then, we can call the instance method in the main method.

Let’s see how we can call the Instance method:

Example 1:

Java




// Java program to see how can we call
// an instance method without parameter
  
import java.io.*;
  
class GFG {
      // static method
    public static void main (String[] args) {  
        
          // Creating object of the class
        GFG obj = new GFG();          
        
          // Calling instance method
        obj.disp();  
        
        System.out.println("GFG!");
    }
        
      // Instance method
    void disp()                                  
    {
          // Local variable
        int a = 20;                              
        System.out.println(a);
    }
}


Output

20
GFG!

Example 2:

Java




// Java program to see how can we call
// an instance method without parameter
  
import java.io.*;
  
// Different class
class class1 {      
    
      // Instance method in different class 
    void add()                
    
      int a= 2;
      int b= 3;
      System.out.println("The sum of 2 and 3 is :" + (a+b));
    }
}
class GFG {
      // Static method
    public static void main (String[] args) {        
        
          // creating object of the class
        class1 obj = new class1();           
            
          // calling instance method
        obj.add();  
            
        System.out.println("GFG!");
    }
}


Output

The sum of 2 and 3 is :5
GFG!

Instance Methods in Java

Instance Methods are the group of codes that performs a particular task. Sometimes the program grows in size, and we want to separate the logic of the main method from other methods. A  method is a function written inside the class. Since java is an object-oriented programming language, we need to write a method inside some classes. 

The important points regarding instance variables are:

  1. Instance methods can access instance variables and instance methods directly and undeviatingly.
  2. Instance methods can access static variables and static methods directly.

Similar Reads

Instance Method without parameter

Syntax:...

Instance Method With Parameter

...

Types of Instance Methods:

...