Static method:
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.
A static method belongs to the class rather than the object of a class.
A static method can be invoked without the need for creating an instance of a class.
A static method can access static data members and can change their value of it.
Restrictions for the Static Method
Advantages of static Keyword
Saves Memory:
Faster Access:
Great for Utility Methods:
Good for Constants:
Keeps Code Organized:
Runs Automatically When the Class Loads:
Reduces Object Dependency:
The static method cannot use non-static data members or call a non-static method directly.
this and super keyword cannot be used in static context.[TBD]
Non Static Method
Non-static members are specific to each instance of a class, as they are tied to objects created from the class.
Non-static members have memory allocated separately for each instance of the class. Each object has its own copy of non-static members
Non-static members are accessed using an object reference followed by the member name
Example:
Ref:
https://www.tpointtech.com/static-keyword-in-java
https://www.scaler.com/topics/static-and-non-static-in-java/