Static & Final Keyword in Java
Static in Java
The static keyword in Java is mainly used for memory management.
In Java, the term static has multiple uses and implications. It is a keyword that can be applied to fields, methods, and nested classes.
The static keyword is a non-access modifier in Java that is applicable for the following:
Variables / Field
Blocks
Methods
Class
1) Static Fields
- If you declare any variable as static, it is known as a static variable.
- The static variable can be used to refer to the common property of all objects .
- The static variable gets memory only once in the class area at the time of class loading.
- It makes your program memory efficient (i.e., it saves memory).
2) Java static method
- If you apply static keyword with any method, it is known as static method.
- A static method belongs to the class rather than the object of a class.
- A static method can be invoked without the need for creating an instance of a class.
- A static method can access static data member and can change the value of it.
- A static methood cannot access instance (i.e. non-static) members of a class.
- A instance method can call an instance or static method. It can also access instance or static data variable.
- A static method can call a static method only. It can only access a static data variable inside the static method.
3) Java static block
- Is used to initialize the static data member/variable.
- It is executed before the main method at the time of classloading.
- It is mostly used for changing default value of static variables.
Why Static block is executed before Main method?
- static block always gets executed first in Java because it is stored in the memory at the time of class loading and before the object creation.
Restrictions for the static method
There are two main restrictions for the static method. They are:
- The static method can not use non static data member or call non-static method directly.
- this and super cannot be used in static context.
- The value of the final variable cannot be changed.
- A final method cannot be overridden.
- A final class cannot be inherited.
- A variable declared with a final keyword is known as a final variable in Java.
- Final variable means a constant (value cannot be modified).
- A final keyword can be applied to local variables, instance variables, and static variables.
- A blank final variable must be initialized in the constructor of the class otherwise we will get a compilation error.
- Once the final variable is initialized in the constructor, it cannot be assigned a new value.
- A method that is declared with the final keyword is known as final method in Java.
- A final method cannot be overridden in Java.
- When a class is extended by another class, its method can be overridden for reuse but if we want to prevent a particular method being overridden.
- A class that is declared with a final keyword is known as final class in Java.
- Final class means Restricting inheritance!. It does not allow itself to be inherited by another class.
Comments
Post a Comment