Home »
Java programming language
Differences between static and non static method in Java
Java static vs non static method: Here, we are going to learn what are the differences between static and non static method in java? Compare static v/s non-static methods?
Submitted by Preeti Jain, on July 08, 2019
static vs non static methods
We will study one by one firstly we will start with static methods and ends with non static methods.
1) static methods
- We must use static keywords to declare or define static methods.
- Static methods are associated with the class that means these methods can be called with the class name or with objects or without objects(Direct call) there is no need to declare any objects for static methods.
- Static methods can access only static member and static methods are of the same class or different class that means static method can’t access non-static member or methods.
- Static methods create only one copy of the whole program and share it with other member or methods.
Example:
// Show the behavior of static member and methods
class StaticClass {
// declare a static method
public static int div(int a, int b) {
return a / b;
}
}
public class Main {
public static void main(String[] args) {
int p = 10, q = 5;
// Declare a static member
String str = "Welcome in Java World";
// Invoking the static method with the classname
int div = StaticClass.div(p, q);
/*
Here we are calling static methods directly without
objects or classname. If we want to call with classname
or with object then also not a problem.
*/
System.out.print("The value of str is = " + str);
System.out.print("The divide of two number is = " + div);
}
}
Output
D:\Programs>javac Main.java
D:\Programs>java Main
The value of str is = Welcome in Java World
The divide of two number is = 2
2) Non-Static methods
- We must not have static keywords before method name to declare or define static methods.
- Non-static methods are not associated with the class that means these methods cannot be called with the class name and it is mandatory to declare objects and non-static methods can be called with the object name.
- Non-static methods can access static member and static methods and non-static member and non-static methods are of the same class or different class but non-static can’t modify static member or methods values.
- For non-static methods create an individual copy of all the objects or in other words create a separate copy of all the objects.
Example:
class NonstaticClass {
// Declare a non-static method
public int div(int a, int b) {
return a / b;
}
}
public class Main {
public static void main(String[] args) {
int p = 10, q = 5;
// Declare an object of NonstaticClass
NonstaticClass nc = new NonstaticClass();
int div = nc.div(p, q);
// Invoking the non-static method with the class object.
System.out.print("The div of two num is = " + div);
}
}
Output
D:\Programs>javac Main.java
D:\Programs>java Main
The div of two num is = 2
TOP Interview Coding Problems/Challenges