Calling process
The calling process for static and non-static methods differs. Static methods can be called directly on the class without requiring an instance of the class. Non-static methods, however, require an instance of the class to be called.
Below is an example showing the calling process for static & non-static methods:
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
public int multiply(int a, int b) {
return a * b;
}
}
public class Main {
public static void main(String[] args) {
// Calling a static method
int sum = Calculator.add(5, 3);
System.out.println("Sum: " + sum);
// Calling a non-static method
Calculator calculator = new Calculator();
int product = calculator.multiply(4, 7);
System.out.println("Product: " + product);
}
}

You can also try this code with Online Java Compiler
Run Code
In this example, the `Calculator` class has a static method called `add` and a non-static method called `multiply`. In the `Main` class, we demonstrate calling the static method `add` directly on the `Calculator` class without creating an instance. To call the non-static method `multiply`, we first create an instance of the `Calculator` class using the `new` keyword and then call the method on that instance.
Output:
Sum: 8
Product: 28
You can clearly see that the static method `add` is called directly on the `Calculator` class, while the non-static method `multiply` requires an instance of the `Calculator` class to be called.
Binding process
The binding process refers to the mechanism by which a method call is associated with the actual method implementation. In Java, static methods are bound at compile time (early binding), while non-static methods are bound at runtime (late binding).
For example :
public class Animal {
public static void staticMethod() {
System.out.println("Animal static method");
}
public void nonStaticMethod() {
System.out.println("Animal non-static method");
}
}
public class Dog extends Animal {
public static void staticMethod() {
System.out.println("Dog static method");
}
public void nonStaticMethod() {
System.out.println("Dog non-static method");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
// Static method binding (compile-time)
Animal.staticMethod();
Dog.staticMethod();
// Non-static method binding (runtime)
animal.nonStaticMethod();
}
}

You can also try this code with Online Java Compiler
Run Code
In this example, we have a base class `Animal`, and a derived class `Dog`. Both classes have a static method, `staticMethod()`, and a non-static method, `nonStaticMethod()`.
When we call the static method `staticMethod()`, the binding happens at compile-time. The compiler determines which implementation of the static method to call based on the class on which it is invoked. In the example, `Animal.staticMethod()` invokes the static method from the `Animal` class, while `Dog.staticMethod()` invokes the static method from the `Dog` class.
On the other hand, when we call the non-static method `nonStaticMethod()` using the `animal` reference, the binding happens at runtime. The actual method implementation to be invoked is determined based on the actual object type (`Dog` in this case), not the reference type (`Animal`). This is known as dynamic dispatch or runtime polymorphism.
Output:
Animal static method
Dog static method
Dog non-static method
As you can see, the static methods are bound at compile-time based on the class they are invoked on, while the non-static method is bound at runtime based on the actual object type.
Overriding process
In Java, method overriding is a mechanism where a subclass provides its own implementation of a method that is already defined in its superclass. However, the overriding process behaves differently for static and non-static methods.
Non-static methods can be overridden in subclasses, allowing the subclass to provide its own implementation of the method. On the other hand, static methods cannot be overridden in the traditional sense. If a subclass defines a static method with the same name & signature as a static method in the superclass, it simply hides or shadows the superclass method rather than overriding it.
For example :
public class Shape {
public static void staticMethod() {
System.out.println("Shape static method");
}
public void draw() {
System.out.println("Drawing a shape");
}
}
public class Circle extends Shape {
public static void staticMethod() {
System.out.println("Circle static method");
}
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
public class Main {
public static void main(String[] args) {
Shape shape = new Circle();
// Static method hiding
Shape.staticMethod();
Circle.staticMethod();
// Non-static method overriding
shape.draw();
}
}

You can also try this code with Online Java Compiler
Run Code
In this example, we have a base class `Shape`, and a derived class `Circle`. The `Shape` class has a static method `staticMethod()` and a non-static method `draw()`. The `Circle` class provides its own implementation of the `draw()` method, effectively overriding it. It also defines a static method `staticMethod()` with the same name as the one in the `Shape` class.
When we call `Shape.staticMethod()`, it invokes the static method from the `Shape` class. Similarly, `Circle.staticMethod()` invokes the static method from the `Circle` class. This demonstrates that static methods are not actually overridden but rather hidden or shadowed in subclasses.
On the other hand, when we call `shape.draw()`, it invokes the overridden `draw()` method from the `Circle` class, even though the reference type is `Shape`. This is because non-static methods are dynamically dispatched based on the actual object type at runtime.
Output:
Shape static method
Circle static method
Drawing a circle
In the above example, we can see that the static methods are not overridden but rather hidden, while the non-static method `draw()` is properly overridden in the subclass.
Memory allocation
Static methods and variables are stored in a special memory area called the "method area" or "class area," which is allocated when the class is loaded into memory by the Java ClassLoader. This memory is shared among all instances of the class and is only allocated once, regardless of how many objects of the class are created.
Non-static methods and variables, on the other hand, are stored in the heap memory and allocated separately for each instance of the class. Each object has its own copy of non-static variables, and non-static methods operate on these instance-specific variables.
For example :
public class MemoryDemo {
private static int staticVariable = 10;
private int instanceVariable;
public static void staticMethod() {
System.out.println("Static method called");
}
public void instanceMethod() {
System.out.println("Instance method called");
System.out.println("Instance variable: " + instanceVariable);
}
public static void main(String[] args) {
// Accessing static variable & method
System.out.println("Static variable: " + MemoryDemo.staticVariable);
MemoryDemo.staticMethod();
// Creating objects & accessing instance variables & methods
MemoryDemo obj1 = new MemoryDemo();
obj1.instanceVariable = 20;
obj1.instanceMethod();
MemoryDemo obj2 = new MemoryDemo();
obj2.instanceVariable = 30;
obj2.instanceMethod();
}
}

You can also try this code with Online Java Compiler
Run Code
In this example, the `MemoryDemo` class has a static variable `staticVariable`, a non-static variable `instanceVariable`, a static method `staticMethod()`, & a non-static method `instanceMethod()`.
The `staticVariable` and `staticMethod()` are stored in the method area and shared among all instances of the `MemoryDemo` class. They can be accessed directly using the class name.
The `instanceVariable` and `instanceMethod()` are stored in the heap memory and are specific to each instance of the `MemoryDemo` class. Each object (`obj1` and `obj2`) has its own copy of `instanceVariable`, and the `instanceMethod()` operates on the instance-specific variable.
Output:
Static variable: 10
Static method called
Instance method called
Instance variable: 20
Instance method called
Instance variable: 30
In this example, we can clearly see that the static variable and method are shared among all instances, while the instance variable and method are specific to each object instance.
Initialization
Static variables and methods are initialized when the class is loaded into memory by the Java ClassLoader. They are initialized in the order they appear in the class definition, from top to bottom. Static variables can be explicitly initialized with default values, and static blocks can be used to perform more complex initialization logic.
Non-static variables are initialized when an instance of the class is created using the `new` keyword. They can be initialized explicitly in the declaration or using instance initializer blocks. Non-static variables are initialized before the constructor is called.
For example:
public class InitializationDemo {
private static int staticVariable = 10;
private static final int STATIC_CONSTANT = 20;
private int instanceVariable = 30;
static {
System.out.println("Static block executed");
staticVariable = 50;
}
{
System.out.println("Instance initializer block executed");
instanceVariable = 60;
}
public InitializationDemo() {
System.out.println("Constructor called");
}
public static void main(String[] args) {
System.out.println("Static variable: " + InitializationDemo.staticVariable);
System.out.println("Static constant: " + InitializationDemo.STATIC_CONSTANT);
InitializationDemo obj = new InitializationDemo();
System.out.println("Instance variable: " + obj.instanceVariable);
}
}

You can also try this code with Online Java Compiler
Run Code
In this example, the `InitializationDemo` class has a static variable, `staticVariable`, a static final variable, `STATIC_CONSTANT`, and a non-static variable, `instanceVariable`.
The `staticVariable` is initialized explicitly with a value of 10. The `STATIC_CONSTANT` is declared as `final` & initialized with a value of 20. Static variables & constants are initialized when the class is loaded.
The class also has a static block that is executed when the class is loaded. It prints a message & updates the value of `staticVariable` to 50.
The `instanceVariable` is initialized explicitly with a value of 30. The class has an instance initializer block that is executed when an instance of the class is created, before the constructor is called. It prints a message and updates the value of `instanceVariable` to 60.
Finally, the class has a constructor that simply prints a message when called.
Output:
Static block executed
Static variable: 50
Static constant: 20
Instance initializer block executed
Constructor called
Instance variable: 60
In this example, the static block is executed when the class is loaded, initializing the static variable. The instance initializer block is executed when an instance of the class is created before the constructor is called, initializing the instance variable.
Scope
The scope of static and non-static members (variables and methods) differs in Java. Static members have a class-level scope, meaning they can be accessed from anywhere within the class, including static and non-static methods. They are shared among all instances of the class.
Non-static members, on the other hand, have an instance-level scope. They are specific to each instance of the class & can only be accessed through an instance of the class. Non-static methods can access both static and non-static members, but static methods can only directly access static members.
For example :
public class ScopeDemo {
private static int staticVariable = 10;
private int instanceVariable = 20;
public static void staticMethod() {
System.out.println("Static method called");
System.out.println("Static variable: " + staticVariable);
// Cannot directly access instanceVariable here
}
public void instanceMethod() {
System.out.println("Instance method called");
System.out.println("Static variable: " + staticVariable);
System.out.println("Instance variable: " + instanceVariable);
}
public static void main(String[] args) {
// Accessing static members
System.out.println("Static variable: " + ScopeDemo.staticVariable);
ScopeDemo.staticMethod();
// Creating an instance & accessing non-static members
ScopeDemo obj = new ScopeDemo();
obj.instanceMethod();
}
}

You can also try this code with Online Java Compiler
Run Code
In this example, the `ScopeDemo` class has a static variable `staticVariable` and a non-static variable `instanceVariable`. It also has a static method `staticMethod()` and a non-static method `instanceMethod()`.
The `staticMethod()` can directly access the `staticVariable` because it has a class-level scope. However, it cannot directly access the `instanceVariable` because it is a non-static member.
The `instanceMethod()` can access both the `staticVariable` and the `instanceVariable` because non-static methods have access to both static and non-static members.
In the `main()` method, we can access the static variable `staticVariable` & the static method `staticMethod()` directly using the class name. To access the non-static method `instanceMethod()`, we need to create an instance of the `ScopeDemo` class using the `new` keyword.
Output:
Static variable: 10
Static method called
Static variable: 10
Instance method called
Static variable: 10
Instance variable: 20
In this example, the static variable and method can be accessed directly using the class name, while the non-static method requires an instance of the class to be accessed.
Frequently Asked Questions
Can static methods be overridden in Java?
No, static methods cannot be overridden in Java. If a subclass defines a static method with the same name & signature as a static method in the superclass, it hides or shadows the superclass method rather than overriding it.
Can non-static methods access static members?
Yes, non-static methods can access both static & non-static members of the class. They have access to all members within their scope.
When should you use static methods in Java?
Static methods are useful for utility functions that don't require access to instance-specific data. They are commonly used for operations that are independent of object state & can be called without creating an instance of the class.
Conclusion
In this article, we discussed the main differences between static & non-static methods in Java. We learned that static methods belong to the class itself, while non-static methods belong to instances of the class. We also explained how they differ regarding accessing members, calling process, binding process, overriding, memory allocation, initialization, & scope.
You can also check out our other blogs on Code360.