What is an static method ?: 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. What is an non-static method ?: 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. Below are the various important differences among these:
Accessing members and methods:
In static method, the method can only access static data members and static methods of another class or same class but cannot access non-static methods and variables. Also a static method can rewrite the values of any static data member.
In non-static method, the method can access static data members and static methods as well as non-static members and method of another class or same class, also can change the values of any static data member.
Calling process:
In static method, 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 name of the method followed by the class name.
class GFG{
public static void geek()
{ }
}
// calling
GFG.geek();
Java
filter_none
edit close
play_arrow
link brightness_4 code
// Java program to call a static method
classGFG {
// static method
publicstaticintsum(inta, intb)
{
returna + b;
}
}
classMain {
publicstaticvoidmain(String[] args)
{
intn = 3, m = 6;
// call the static method
ints = GFG.sum(n, m);
System.out.print("sum is = "+ s);
}
}
chevron_right
filter_none
Output:
sum is = 9
In non-static method, the memory of non-static method is not fixed in the ram, so we need 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.
class GFG{
public void geek()
{ }
}
// creating object
GFG g = new GFG();
g.geek();
// calling
Java
filter_none
edit close
play_arrow
link brightness_4 code
// Java program to call a non-static method
classGFG {
// non-static method
publicintsum(inta, intb)
{
returna + b;
}
}
classMain {
publicstaticvoidmain(String[] args)
{
intn = 3, m = 6;
GFG g = newGFG();
ints = g.sum(n, m);
// call the non-static method
System.out.print("sum is = "+ s);
}
}
chevron_right
filter_none
Output:
sum is = 9
Binding process:
In static method, the method use compile time or early binding. For this reason we can access static method without creation a instance.
In non-static method, the method use runtime or dynamic binding. So that we cannot access a non-static method without creation a instance.
Overriding:
In static method, we cannot override a static method, because of early binding. Example:
Java
filter_none
edit close
play_arrow
link brightness_4 code
// Override of static method
classParent {
// static method
staticvoidshow()
{
System.out.println("Parent");
}
}
// Parent inherit in Child class
classChild extendsParent {
// override show() of Parent
voidshow()
{
System.out.println("Child");
}
}
classGFG {
publicstaticvoidmain(String[] args)
{
Parent p = newParent();
// calling Parent's show()
p.show();
// cannot override Parent's show()
}
}
chevron_right
filter_none
Output:
java:15: error: show() in Child cannot override show() in Parent
void show()
^
overridden method is static
In non-static method, we can override a non-static method. Because for override we need runtime polymorphism, which is happens only in runtime binding. Example:
Java
filter_none
edit close
play_arrow
link brightness_4 code
// Override of non-static method
classParent {
voidshow()
{
System.out.println("Parent");
}
}
// Parent inherit in Child class
classChild extendsParent {
// override show() of Parent
voidshow()
{
System.out.println("Child");
}
}
classGFG {
publicstaticvoidmain(String[] args)
{
Parent p = newParent();
// calling Parent's show()
p.show();
Parent c = newChild();
// calling Child's show()
c.show();
}
}
chevron_right
filter_none
Error:
Parent
Child
Memory allocation:
In 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 the less memory is allocated.
In 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.
Difference table:
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 methods in java defaults to non-static method without 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 static method, the method can only access only static data members and static methods of another class or same class but cannot access non-static methods and variables.
In non-static method, the method can access static data members and static methods as well as non-static members and method of another class or same class.
Binding process
Static method uses compile time or early binding.
Non-static method uses runtime or dynamic binding.
Overriding
Static method cannot be overridden because of early binding.
Non-static method can be overridden because of runtime binding.
Memory allocation
In static method, less memory is use for execution because memory allocation happens only once, because the static keyword fixed a particular memory for that method in ram. .
In 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.
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy