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


  1. If you declare any variable as static, it is known as a static variable.
  2. The static variable can be used to refer to the common property of all objects . 
  3. The static variable gets memory only once in the class area at the time of class loading. 
  4. It makes your program memory efficient (i.e., it saves memory).


2) Java static method

  1. If you apply static keyword with any method, it is known as static method.
  2. A static method belongs to the class rather than the object of a class.
  3. A static method can be invoked without the need for creating an instance of a class.
  4. A static method can access static data member and can change the value of it.
  5. A static methood cannot access instance (i.e. non-static) members of a class.
  6. A instance method can call an instance or static method. It can also access instance or static data variable.
  7. 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

  1. Is used to initialize the static data member/variable.
  2. It is executed before the main method at the time of classloading.
  3. 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.


---------------------------------------------------------------------------------------------------------------------------
Final Keyword in Java

The final keyword declared with variable, method, and class indicates that This cannot be modified .
or We cannot change final we they declare as a final.

  • The value of the final variable cannot be changed.
  • A final method cannot be overridden.
  • A final class cannot be inherited.


Java Final keyword has three different uses:

1. To declare a constant.
2. To prevent inheritance.
3. To prevent method from being overridden.


1. Final Variable in Java
  • 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.

When to use Final variable in Java?
A final variable can be used where we want to remain constant the value of a variable throughout the execution of a program.


2.Final Method in Java
  • 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.


3.Final Class in Java
  • 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.

Use of Final class ?
Java classes declared as a final cannot be extended (inherited). If you do not want to be a subclass, declare it final. A lot of classes in Java API are final.


Some Key Points:
1. A constructor cannot be final.
2. A block cannot be final.
3. A local final variable must be initialized at the time of declaration.
4. We cannot change the value of a final variable after initialization.
5. A final method cannot be overridden.
6. A final class cannot be extended(inherited).
7. We can create the object for a final class but cannot extend it.
8. If the method parameters are declared as final, the value of these parameters cannot be changed.


Comments

Popular Posts