Friday, December 27, 2024
Google search engine
HomeLanguagesJavaDifference between static and non-static method in Java

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 creating an instance of the object.

Difference between Static and Non-Static Methods

Let us clarify the differences Below are the various important differences among these pointers as follows:

  1. Accessing members and methods
  2. Calling process
  3. Binding process
  4. Overriding process
  5. Memory allocation

1. Accessing members and methods

A static method can only access static data members and static methods of another class or the same class but cannot access non-static methods and variables. Also, a static method can rewrite the values of any static data member.

A non-static method can access static data members and static methods as well as non-static members and methods of another class or the same class, and also can change the values of any static data member

Example 1:

Java




// Java program to Illustrate Calling of a Static Method
 
// Class 1
// Helper class
class Helper {
 
    // Static method
    public static int sum(int a, int b)
    {
        // Simply returning the sum
        return a + b;
    }
}
 
// Class 2
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Custom values for integer
        // to be summed up
        int n = 3, m = 6;
 
        // Calling the static method of above class
        // and storing sum in integer variable
        int s = Helper.sum(n, m);
 
        // Print and display the sum
        System.out.print("sum is = " + s);
    }
}


Output

sum is = 9

Example 2:

Java




// Java program to Illustrate Calling of a Non-Static Method
 
// Class 1
// Helper class
class Helper {
 
    // Non-static method
    public int sum(int a, int b)
    {
        // Returning sum of numbers
        return a + b;
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Input integers to be summed up
        int n = 3, m = 6;
 
        // Creating object of above class
        Helper g = new Helper();
 
        // Calling above method to compute sum
        int s = g.sum(n, m);
 
        // Calling the non-static method
        System.out.print("sum is = " + s);
    }
}


Output

sum is = 9

2. Calling process 

The memory of a static method is fixed in the ram, for this reason, we don’t need the object of a class in which the static method is defined to call the static method. To call the method we need to write the class name followed by the name of the method

Syntax: Calling of static methods

class GFG{
 public static void geek()
 { }
}

// calling
GFG.geek();

The memory of the non-static method is not fixed in the ram, so we need a class object to call a non-static method. To call the method we need to write the name of the method followed by the class object name

Syntax: Calling of non-static methods

class GFG{
 public void geek()
 { }
}

// creating object
GFG g = new GFG();

// calling
g.geek();

3. Binding process 

In the static method, the method use compile-time or early binding. For this reason, we can access the static method without creating an instance. In a non-static method, the method use runtime or dynamic binding. So that we cannot access a non-static method without creating an instance. 

4. Overriding 

In the static method, we cannot override a static method, because of early binding. 

Example 1:

Java




// Override of static method
class Parent {
 
    // static method
    static void show()
    {
        System.out.println("Parent");
    }
}
 
// Parent inherit in Child class
class Child extends Parent {
 
    // override show() of Parent
    void show()
    {
        System.out.println("Child");
    }
}
 
class GFG {
    public static void main(String[] args)
    {
        Parent p = new Parent();
        // calling Parent's show()
        p.show();
        // cannot override Parent's show()
    }
}


Output: 

java:15: error: show() in Child cannot override show() in Parent
    void show()
         ^
  overridden method is static

In the non-static method, we can override a non-static method. Because for override we need runtime polymorphism, which happens only in runtime binding. 

Example 2:

Java




// Override of non-static method
 
class Parent {
    void show()
    {
        System.out.println("Parent");
    }
}
 
// Parent inherit in Child class
class Child extends Parent {
 
    // override show() of Parent
    void show()
    {
        System.out.println("Child");
    }
}
 
class GFG {
    public static void main(String[] args)
    {
        Parent p = new Parent();
        // calling Parent's show()
        p.show();
 
        Parent c = new Child();
        // calling Child's show()
        c.show();
    }
}


Output: Error

Parent
Child

5. Memory allocation 

In the static method, memory allocation happens only once, because the static keyword fixed a particular memory for that method in ram. So when the method is called every time in a program, each time that particular memory is used. For that reason, less memory is allocated.

In the non-static method, here memory allocation happens when the method is invoked and the memory is allocated every time when the method is called. So much memory is used here. Now, lastly plotting table in order to grasp altogether

Points

Static method

Non-static method

Definition 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 a static keyword preceding it. non-static methods can access any static method and static variable also, without using the object of the class.
Accessing members and methods In the static method, the method can only access only static data members and static methods of another class or the same class but cannot access non-static methods and variables. In the non-static method, the method can access static data members and static methods as well as non-static members and methods of another class or the same class. 
Binding process The static method uses compile-time or early binding. The non-static method uses runtime or dynamic binding.
Overriding The static method cannot be overridden because of early binding. The non-static method can be overridden because of runtime binding.
Memory allocation In the static method, less memory is used for execution because memory allocation happens only once because the static keyword fixed a particular memory for that method in ram. In the non-static method, much memory is used for execution because here memory allocation happens when the method is invoked and the memory is allocated every time when the method is called.
Previous article
Next article
RELATED ARTICLES

Most Popular

Recent Comments