Posts

Showing posts with the label static

Static Keyword in Java

Image
- Mainly used for Memory Management - Static variables are stored in non-heap memory, Non-static variables are stored in heap memory - Usages         1. Block         2. Variable         3. Method         4. Nested class Static Block - It's executed exactly once when the class is first loaded - It's executed before the main method at the time of classloading. Example: public class StaticKeyword { static { System.out.println("Static block initialized."); } public static void main(String[] args) { System.out.println("Main Method"); } } Response: Static block initialized. Main Method Static Variable - It's used for a constant variable or a method that is same for every instance of a class - It can be used to refer to the common property of all objects - It can be created at class-level only. (Not allowed to create a static local variable in member func...