What’s the purpose of Static methods and static variables in Java?
Asked
7 years ago
Viewed
610 times
What’s the purpose of Static methods and static variables in Java?
1 Answer
The method and variables marked as static when it belongs to a class rather than to any particular instance of the class. During development of the project when there is a need to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects. In Java, only the class is adequate to invoke a static method or access a static variable.Where a static variable is shared by all the instances of that class, means only one copy of the static variable is maintained.