Difference between Static and Non-static in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

In Java, the difference between static and non-static methods plays a fundamental role in defining the behavior and structure of classes. Understanding the differences between static and non-static methods is crucial for effective Java programming.

A static method is one that belongs to a class instead of an instance of a class and this method can be called without an instance or the object of the class. If a static keyword is not mentioned before the class name then it is by default a non-static class.

Non-static methods can access any method that is static and variable even without creating an object.

Below are the different parameters of difference between static and non-static:

  • Associated with
  • Accessing methods and members
  • Initialization
  • Scope
  • Usage
  • Calling process
  • Binding process
  • Overriding process
  • Memory Allocation

1. Associated with

The difference between static and non-static in Java with respect to associated with

  • Static: Associated with the class itself, not with any instance of the class.
  • Non-Staic: Associated with the instance of the class.

2. Accessing Members and Methods

In order to access a member of a class from a different class, do the following things:

  1. Import the class
  2. Create an object of that class
  3. Access the members of that class using the object For example: Let’s say there is a class in the package named, myPack which has a method display.

Now to access this class in some other class, just import the package of the class.

This is how another class in a different directory can be accessed by just importing the package.

Output

3. Initialization of Static and Non-static Methods

  • Static: Static methods are initialized when the class is loaded into the memory.
  • Non-static: Initialized when an instance of the class is created.

4. Scope of Static and Non-Static Methods

  • Static: The Scope of static method is throughout the program's execution.
  • Non-Static: Scope of non-static method is limited to the instance of the class.

5. Usage of Static and Non-Static Methods

  • Static: Static Methods are used for operations that do not require instance-specific data.
  • Non-Static: Non-Static methods are used for operations that require instance-specific data.

6. Calling Process

Calling static method

In this example, we will be learning how the main method calls the static method.

Output

Explanation: In this, the method ‘prod’ is static and it calculates the multiplication of two variables a and b, and returns the result. Now if we remove the word ‘static’ then the error will occur. The demo of it is given below. The reason for this error is that a static method can only be accessed by static objects and vice-versa. By removing the word static, the method becomes non-static and hence the static object is unable to access the method.

Output:

calling static method

Calling a non-static method

Output:

Explanation: In this, if we want to non-static method in the main method, then we need to create an instance of the Calculate class.

7. Binding Process

It is a mechanism for creating a link between the method which is implemented and the actual method. There are two types of binding in java:

  1. Static binding
  2. Dynamic binding

Static binding: If the linking between method implementation and method call is resolved during compilation time then it is called static binding.

Dynamic binding: If the linking between method implementation and method call is resolved during runtime, then it is called dynamic binding.

Example: Implementation of static and dynamic binding.

Output

8. Overriding Process

Overriding in Java is a type of feature in java that allows child class or subclass to implement methods of parent class or super class by overriding some constraints. By overriding methods, java can achieve run time polymorphism. Let’s understand this by using an example: Implementation of overring methods in Java

Output

Explanation: In this example, a parent class object is instantiated and the method is invoked using the object. After this, a child class object is instantiated with reference to paren class and the method is called.

9. Memory Allocation

The memory for static and non-static methods are: Static - In static methods, the memory allocation is done only once since only static methods can access the static variable, which happens once at the time of class loading. Non-static - non-static are also called instance methods. Every time a method is called for a particular class instance, the memory is allotted.

Static vs Non-static Method

This table summarizes the key differences between static and non-static methods in Java or similar object-oriented programming languages.

AspectStatic MethodsNon-static Methods
Associated withClassInstance
Accessing Members and MethodsCan only directly access static members and methods of the class.Can directly access both static and non-static members and methods of the class.
InitializationInitialized when the class is loaded into memory.Initialized when an instance of the class is created.
ScopeAvailable throughout the program executionAvailable as long as the instance exists
UsageUseful for utility methods, where the method does not depend on instance-specific data.Useful for operations that require access to instance-specific data.
Calling ProcessCan be called using the class name.Must be called using an instance of the class.
Binding ProcessBound at compile-time.Bound at runtime.
Overriding ProcessCannot be overridden.Can be overridden in subclasses.
Memory AllocationMemory is allocated only once for the entire program execution.Memory allocated each time an instance is created.

Conclusion

  • Association: Static methods are associated with the class itself, while non-static methods are associated with instances of the class.
  • Access: Static methods can only directly access static members, while non-static methods can access both static and non-static members.
  • Initialization: Static methods are initialized when the class is loaded into memory, whereas non-static methods are initialized when an instance of the class is created.
  • Scope: Static methods have a global scope throughout program execution, while non-static methods have a scope limited to the instance of the class.
  • Usage: Static methods are suitable for utility operations independent of instance data, whereas non-static methods are used for operations depending on instance-specific data.
  • Calling Process: Static methods can be called using the class name, while non-static methods must be called using an instance of the class.
  • Binding Process: Static binding occurs at compile-time for static methods, while dynamic binding happens at runtime for non-static methods.
  • Overriding: Static methods cannot be overridden, whereas non-static methods can be overridden in subclasses.
  • Memory Allocation: Static methods allocate memory once for the entire program, whereas non-static methods allocate memory each time an instance is created.