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:
- Accessing members and methods
- Calling process
- Binding process
- Overriding process
- 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
class Helper {
public static int sum( int a, int b)
{
return a + b;
}
}
class GFG {
public static void main(String[] args)
{
int n = 3 , m = 6 ;
int s = Helper.sum(n, m);
System.out.print( "sum is = " + s);
}
}
|
Example 2:
Java
class Helper {
public int sum( int a, int b)
{
return a + b;
}
}
class GFG {
public static void main(String[] args)
{
int n = 3 , m = 6 ;
Helper g = new Helper();
int s = g.sum(n, m);
System.out.print( "sum is = " + s);
}
}
|
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
class Parent {
static void show()
{
System.out.println( "Parent" );
}
}
class Child extends Parent {
void show()
{
System.out.println( "Child" );
}
}
class GFG {
public static void main(String[] args)
{
Parent p = new Parent();
p.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
class Parent {
void show()
{
System.out.println( "Parent" );
}
}
class Child extends Parent {
void show()
{
System.out.println( "Child" );
}
}
class GFG {
public static void main(String[] args)
{
Parent p = new Parent();
p.show();
Parent c = new Child();
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. |
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
30 Jun, 2023
Like Article
Save Article