The Static Keyword is a non-access modifier in Java mainly used for memory management. This static keyword is mainly applicable for the following:
- Nested Class
Static Variable is a variable that is declared as static means a single copy of the variable is created and shared among all objects at the class level. Static method is a method belonging to the class rather than the object of a class as well as a static method can be invoked without the need for creating an instance of a class.
- The static variable gets memory only once in the class
- A static method can access static data members and can change the value of it.
Now here we will be discussing out both the cases with a simple demonstration via clean java programs as follows:
- Case 1: Accessibility of a static method/s by static variable/s
- Case 2: Accessibility of a static variable by static method
Case 1: Accessibility of the static methods by static variables
Declaring the variable as static and after that, we called that variable in a static method so that we get that variable as an output.
Static Variable = 10
Case 2: Accessibility of a static variable by static method
Declaring the variable, array, and method as static where the static variable and the static array are accessed by the static method. The static method is called without creating an instance of the class.
Static variable : 10 Array = 20 60 30 0 10 70
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.