Static Keyword -Java
What is a static keyword?
The static keyword is basically a non-access modifier
We can use it with class-level variable
We can use methods
We can make a static block
We can use static with inner class too
Why do we create static variables?
static variables' main use is for memory management when a variable is declared as static then a single copy of the variable is created and shared among all the objects at the class level.
We create static variables when we want to make our program memory efficient
And when we have some variable common for all the objects, then we make that object static
Static Methods
Static methods belong to class not object
Static methods can be called directly by class name
Static Block
What is a static block?
The static block gets executed automatically when a class is loaded in the memory
We need a main method to execute a static block in java1.8
But in java version 1.6 and older we can execute static block without main method