The Wayback Machine - https://web.archive.org/web/20250304074824/https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
Open In App

Static Method vs Instance Method in Java

Last Updated : 02 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In Java, methods are mainly divided into two parts based on how they are associated with a class, which are the static method and the Instance method. The main difference between static and instance methods is:

  • Static method: This method belongs to the class and can be called without creating an object.
  • Instance method: This method belongs to an object and requires an object to be called.

Static Method vs Instance Method

The following table lists the major differences between the static methods and the instance method in Java.

Features

Static method

Instance method

Definition

Created using the static keyword and retrieved without creating an object.

Requires an object of its class to be invoked.

Access

Access only static variables and methods.

Can access both static and instance members.

this keyword

Cannot use the this keyword within static methods.

Can use the this keyword to refer to the current object.

Override

Does not support runtime polymorphism

Supports runtime polymorphism

Static Method

A static method can created using the static keyword. It can be called without creating an object of the class, referenced by the class name itself or reference to the Object of that class. 

Memory Allocation of Static Methods: Static methods belong to the class, not its objects, and they are stored in the Permanent Generation space of the heap. Their local variables and arguments are stored in the stack. They can be called without creating an instance of the class, using ClassName.methodName(args).

Important Points:

  • Static methods are shared among all objects of the class.
  • They cannot be overridden as they use static binding at compile time.
  • If both superclass and subclass have static methods with the same name, it is called Method Hiding, where the subclass method hides the superclass method.

Example:

Java
// Demonstration of the static method  
import java.io.*;

class Geeks {
  
  // static method 
  public static void greet(){
    
    System.out.println("Hello Geek!");
  }
    public static void main (String[] args) {
      
       // calling the method directily
       greet();
      
        // calling the method 
        // using the class name
        Geeks.greet();  
    }
}

Output
Hello Geek!
Hello Geek!

Instance Method

Instance methods are the 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.

Memory Allocation of Instance Method: Instance methods are stored in the Permanent Generation space of the heap (till Java 7, replaced by Metaspace from Java 8 for better efficiency). Their parameters, local variables, and return values are allocated on the stack. They can be called within their class or from other classes, based on their access modifiers.

Important Points:

  • Instance methods belong to the object, not the class, and require an object to be called.
  • They are stored in one memory location and identify their object through the this pointer.
  • They can be overridden as they use dynamic binding at runtime.

Example:

Java
// Demonstrating the in use of instance method
import java.io.*;

class Test {
    String n = "";

    // Instance method 
    public void test(String n) { 
      this.n = n; 
    }
}

class Geeks {
    public static void main(String[] args) {

        // create an instance of the class
        Test t = new Test();

        // calling an instance method 
        // in the class 'Geeks'
        t.test("GeeksforGeeks");
        System.out.println(t.n);
    }
}

Output
GeeksforGeeks

FAQs 

What if static variable refers to an Object? 

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

  • Value of i: Stored in Metaspace (PermGen in Java 7 and earlier).
  • Reference obj: Stored in Metaspace, but the object it refers to is stored in the heap.

When to use static methods? 

Use static methods when:

  • The method does not depend on the state of an object.
  • Code can be shared across all objects of a class (e.g., utility methods).

Can we call static method directly without creating an object?

Yes, static methods can be called directly if they are in the same class, or using the class name if they are in a different class.

How to decide which method to use static or instance?

  • Use static methods for class-level operations or utilities.
  • Use instance methods for operations that depend on the state of an object.

Kickstart your Java journey with our online course on Java Programming, covering everything from basics to advanced concepts. Complete real-world coding challenges and gain hands-on experience. Join the Three 90 Challenge—finish 90% in 90 days for a 90% refund. Start mastering Java today!


Next Article
Article Tags :
Practice Tags :

Similar Reads

How to Check the Accessibility of the Static and Non-Static Variables by a Static Method?
The static keyword is a non - access modifier in Java which can be used for variables, methods, and block of code. Static variables in Java belong to the class i.e it is initialized only once at the start of the execution. By using static variables a single copy is shared among all the instances of the class, and they can be accessed directly by cl
3 min read
Java Program to Check the Accessibility of an Instance variable by a Static Method
A static method belongs to the class rather than the object of a class. It can be invoked without the need for creating an instance of a class. It can access static data member and can change the value of it. The static keyword is a non – access modifier in Java which can be used for variables, methods, and block of code. Static variables in Java b
3 min read
Difference between static and non-static method in Java
A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class. Every method in java defaults to a non-static method without static keyword preceding it. Non-static methods can access any static method and static variable, without cr
6 min read
Why non-static variable cannot be referenced from a static method in Java
Java is one of the most popular and widely used programming language and platform. Java is Object Oriented. However, it is not considered as a pure object-oriented as it provides support for primitive data types (like int, char, etc). In java, methods can be declared as either static or non-static. In this article, let's discuss why non-static vari
4 min read
Java Program to Check the Accessibility of an Static Variable By a Static Method
The Static Keyword is a non-access modifier in Java mainly used for memory management. This static keyword is mainly applicable for the following: VariablesMethodsBlocksNested Class Static Variable is a variable that is declared as static means a single copy of the variable is created and shared among all objects at the class level. Static method i
3 min read
Difference between static and non-static variables in Java
There are three types of variables in Java: Local VariablesInstance VariablesStatic Variables The Local variables and Instance variables are together called Non-Static variables. Hence it can also be said that the Java variables can be divided into 2 categories: Static Variables: When a variable is declared as static, then a single copy of the vari
4 min read
Difference Between Static and Non Static Nested Class in Java
Nested classes are divided into two categories namely static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. A class can either be static or non-static in java. So there is a lot of difference between making a class static or non-static. There are two kind
4 min read
Static and non static blank final variables in Java
A variable provides us with named storage that our programs can manipulate. There are two types of data variables in a class: Instance variables : Instance variables are declared in a class, but outside a method, constructor or any block. When a space is allocated for an object in the heap, a slot for each instance variable value is created. Instan
5 min read
Understanding "static" in "public static void main" in Java
Following points explain what is "static" in the main() method: main() method: The main() method, in Java, is the entry point for the JVM(Java Virtual Machine) into the java program. JVM launches the java program by invoking the main() method. Static is a keyword. The role of adding static before any entity is to make that entity a class entity.
3 min read
Understanding storage of static methods and static variables in Java
In every programming language, memory is a vital resource and is also scarce. Hence the memory must be managed thoroughly without any leaks. Allocation and deallocation of memory is a critical task and requires a lot of care and consideration. In this article, we will understand the storage of static methods and static variables in Java. Java Virtu
5 min read