Java Final Keyword

Java Final Keyword


Final Keyword In Java
Java’s final keyword has slightly different meanings depending on the context, but in general, it says “This cannot be changed.” You might want to prevent changes for two reasons: design or efficiency. Because these two reasons are quite different, it’s possible to misuse the final keyword. In other words - 
A variable can be declared as final. Doing so prevents its contents from being modified.
This means that you must initialize a final variable when it is declared.
The keyword final can also be applied to methods, but its meaning is substantially different than when it is applied to variables.
It is a common coding convention to choose all uppercase identifiers for final variables.
Variables declared as final do not occupy memory on a per-instance basis.
We should use the final keyword especially when we have to store a constant value like the value of PI = 3.14…
Thus, a final variable is essentially a constant.
Now we see the use of final keyword with the help of an example - 
Example - 
package javaLearnings;

class T{
  final String name = "Web designing House";
}
public class Main {
      public static void main (String[] args){
      final int a = 10;
      T ob = new T();
      ob.name = "Web designing house";
      a = 3;
      System.out.println("the value of a = "+a);
      System.out.println(ob.name);

      }
  }
Output -