Differences between static and non-static methods in Java

JavaObject Oriented ProgrammingProgramming

Static Method

The static keyword is used to create methods that will exist independently of any instances created for the class. Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables. Class variables and methods can be accessed using the class name followed by a dot and the name of the variable or method. static keyword is used to mark a method as static.

public static void roar(){
   ...
}

Non-Static Method

Any method of a class which is not static is called non-static method or an instance method. To access a non-static method, we need an object instance to access it.

public void eat(){
   ...
}

Differences

Following are the important differences between static and non-static method.

Sr.No. Key Static Non-Static
1 Access A static method can access only static members and can not access non-static members. A non-static method can access both static as well as non-static members.
2 Binding Static method uses complie time binding or early binding. Non-static method uses run time binding or dynamic binding.
3 Overriding A static method cannot be overridden being compile time binding. A non-static method can be overridden being dynamic binding.
4 Memory allocation Static method occupies less space and memory allocation happens once. A non-static method may occupy more space. Memory allocation happens when method is invoked and memory is deallocated once method is executed completely.
5 Keyword A static method is declared using static keyword. A normal method is not required to have any special keyword.

Example

Following is an example where we've defined one static and one non-static method. Static method is accessed using class name and non-static method is accessed using the object as shown below −

JavaTester.java

public class JavaTester { public static void main(String args[]) { Tiger.roar(); Tiger tiger = new Tiger(); tiger.eat(); } } class Tiger { public void eat(){ System.out.println("Tiger eats"); } public static void roar(){ System.out.println("Tiger roars"); } }

Output

Tiger roars
Tiger eats

Example

Following is an example where we've defined one static and one non-static method. Both methods are accessed using object as shown below −

JavaTester.java

public class JavaTester { public static void main(String args[]) { Tiger tiger = new Tiger(); tiger.roar(); tiger.eat(); } } class Tiger { public void eat(){ System.out.println("Tiger eats"); } public static void roar(){ System.out.println("Tiger roars"); } }

Output

Tiger roars
Tiger eats

Static method is common across the objects created as it is available at class level and can be accessed using class name where as non-static method, is object level and can be accessed only via object. Generally utility functions are made as static methods.

raja
Updated on 19-Aug-2022 12:53:03

Advertisements