- The static keyword can be used with class, variable, method and block.
- Static members belong to the class instead of a specific instance, this means if you make a member static, you can access it without an object.
- A static block is used for initializing the static variables. This block gets executed when the class is loaded in the memory.
- A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.
- A single copy of static variable is created and shared among all the instances of the class.
- Memory allocation for such variables only happens once when the class is loaded in the memory.
- Static Methods can access class variables(static variables) without using object(instance) of the class.
- Syntax: static return_type method_name();
- A class can be made static only if it is a nested class.
- Nested static class doesn’t need a reference of Outer class.
- A static class cannot access non-static members of the Outer class.
- All the static member variables are kept on the Permanent Generation area
- Code inside a static block is executed only once: the first time you make an object of that class or the first time you access a static member of that class.
- Static blocks are executed before constructors.
- Static Block is also called before Instance Initialization Block
- This example will help you to understand the above 3 points.