Open In App

Static methods vs Instance methods in Java

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn about Static Methods and Instance Methods in Java.

Java Instance Methods

Instance methods are methods that require an object of its class to be created before it can be called. To invoke an instance method, we have to create an Object of the class in which the method is defined. 

public void geek(String name)
{
 // code to be executed....
}
// Return type can be int, float String or user defined data type.

Memory Allocation of Instance Method

These methods themselves are stored in the Permanent Generation space of the heap (Valid till Java 7 only, now replaced with metaspace from Java 8 and onwards with improved efficiency) but the parameters (arguments passed to them) and their local variables and the value to be returned are allocated in stack. They can be called within the same class in which they reside or from the different classes defined either in the same package or other packages depending on the access type provided to the desired instance method.

Important Points: 

  • Instance method(s) belong to the Object of the class, not to the class i.e. they can be called after creating the Object of the class.
  • Instance methods are not stored on a per-instance basis, even with virtual methods. They’re stored in a single memory location, and they only “know” which object they belong to because this pointer is passed when you call them.
  • They can be overridden since they are resolved using dynamic binding at run time.

Below is the implementation of accessing the instance method:

Java




// Example to illustrate accessing the instance method .
import java.io.*;
 
class Foo {
 
    String name = "";
 
    // Instance method to be called within the
    // same class or from a another class defined
    // in the same package or in different package.
    public void geek(String name) { this.name = name; }
}
 
class GFG {
    public static void main(String[] args)
    {
 
        // create an instance of the class.
        Foo ob = new Foo();
 
        // calling an instance method in the class 'Foo'.
        ob.geek("GeeksforGeeks");
        System.out.println(ob.name);
    }
}


Output

GeeksforGeeks

Java Static Methods

Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.  

public static void geek(String name)
{
 // code to be executed....
}
// Must have static modifier in their declaration.
// Return type can be int, float, String or user defined data type.

Memory Allocation of Static Methods

They are stored in the Permanent Generation space of heap as they are associated with the class in which they reside not to the objects of that class. But their local variables and the passed argument(s) to them are stored in the stack. Since they belong to the class, so they can be called to without creating the object of the class.

Important Points:  

  • Static method(s) are associated with the class in which they reside i.e. they are called without creating an instance of the class i.e ClassName.methodName(args).
  • They are designed with the aim to be shared among all objects created from the same class.
  • Static methods can not be overridden, since they are resolved using static binding by the compiler at compile time. However, we can have the same name methods declared static in both superclass and subclass, but it will be called Method Hiding as the derived class method will hide the base class method.

Below is the illustration of accessing the static methods: 

Java




// Example to illustrate Accessing
// the Static method(s) of the class.
import java.io.*;
 
class Geek {
 
    public static String geekName = "";
 
    public static void geek(String name)
    {
        geekName = name;
    }
}
 
class GFG {
    public static void main(String[] args)
    {
 
        // Accessing the static method geek()
        // and field by class name itself.
        Geek.geek("vaibhav");
        System.out.println(Geek.geekName);
 
        // Accessing the static method geek()
        // by using Object's reference.
        Geek obj = new Geek();
        obj.geek("mohit");
        System.out.println(obj.geekName);
    }
}


Output

vaibhav
mohit

Note:

Static variables and their values (primitives or references) defined in the class are stored in PermGen space of memory. 

Frequently Asked Questions 

1. What if static variable refers to an Object? 

static int i = 1;
static Object obj = new Object();

In the first line, the value 1 would be stored in PermGen section. In second line, the reference obj would be stored in PermGen section and the Object it refers to would be stored in heap section.

2. When to use static methods? 

  • When you have code that can be shared across all instances of the same class, put that portion of code into static method.
  • They are basically used to access static field(s) of the class.

3. Difference Between Instance method vs Static method

  • Instance method can access the instance methods and instance variables directly.
  • Instance method can access static variables and static methods directly.
  • Static methods can access the static variables and static methods directly.
  • Static methods can’t access instance methods and instance variables directly. They must use reference to object. And static method can’t use this keyword as there is no instance for ‘this’ to refer to.
     


Last Updated : 11 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads