on Leave a Comment

Java Final Keyword

The java variables, methods and classes can be declared using final keyword. Final keyword is a modifier and can be used in different context. A variable that is declared with final keyword and not initialized is called blank final variable. Final keyword can be used with:

- Instance Variable
- Static Variable
- Local Variable
- Method 
- Class

Final Instance Variable

Instance variables declared with final keyword can't be modified. We can't change the value of final instance variable. 

Final instance variable can't be
- Modified
- Inherited

Example of Final Instance Variable

class Example
{
 public final int x = 5;
}
class FinalKeyword
{
 public static void main(String[] args) {
  Example obj = new Example();
  
  obj.x = 10;  //Compile Error 
 }
}

Output:

FinalKeyword.java:11: error: cannot assign a value to final variable x
                obj.x = 10;  //Compile Error
                   ^
1 error

If we don't assign a value to final instance variable while declaring it, then it can be only initialized by constructor and initialization block.

Example:

class Example
{
 public final int x;
 //Initialization Block
 {
  x = 10;
  }
}
class FinalKeyword
{
 public static void main(String[] args) {
  Example obj = new Example();
  System.out.println(obj.x);
 }
}

Output:

10

Final Static Variable

A static variable is made once for a class. Static variable can be declared with final keyword. Final static variable can't be modified after assigning. Static variable in java does not inherit. 

Example of Final Static Variable

class Example
{
 public final static int x = 10;
}
class FinalKeyword
{
 public static void main(String[] args) {
  System.out.println(Example.x);
 }
}

Output:

10

If we don't initialize a final static variable while declaring, then it can only be assigned by static block. 

Example:

class Example
{
 public final static int x;
 static
 {
  x = 10;
  }
}
class FinalKeyword
{
 public static void main(String[] args) {
  System.out.println(Example.x);
 }
}

Output:

10


Final Local Variable 

A local variable is declared within a constructor, method or block. Local variable can be declared with final keyword. After assigning a value to final local variable, its value can't be changed.

Example of Final Local Variable

class Example
{
 public int show()
 {
  final int x = 20;
  return x;
  }
}
class FinalKeyword
{
 public static void main(String[] args) {
  Example obj = new Example();
  System.out.println(obj.show());
 }
}

Output:

20

If we don't assign a value to final local variable while declaring, then it can be only initialized by initialization block.


Example:

class Example
{
 public int show()
 {
  final int x;
  {
   x = 20;
  }
  return x;
  }
}
class FinalKeyword
{
 public static void main(String[] args) {
  Example obj = new Example();
  System.out.println(obj.show());
 }
}

Output:

20


Final Method

A method can be declared with final keyword. A method declared with final keyword is called final method. Final method can't be overriden.

Example of Final Method

class Example
{
 final void show()
 {
  System.out.println("Final method cannot be overriden");
  }
}
class FinalKeyword extends Example
{
 void show()
 {
  System.out.println("Cannot be performed");
 }
 public static void main(String[] args) {
  FinalKeyword obj = new FinalKeyword();

  obj.show();
 }
}

Output:

FinalKeyword.java:10: error: show() in FinalKeyword cannot override show() in Ex
ample
        void show()
             ^
  overridden method is final
1 error

Final Class

A class declared with final keyword is called final class. A final class cannot be inherited.

Example of Final Class

final class Example
{
}
class FinalKeyword extends Example  //Compilation Error
{
 public static void main(String[] args) {
  System.out.println("Final class cannot be extended");
 }
}

Output:

FinalKeyword.java:4: error: cannot inherit from final Example
class FinalKeyword extends Example  //Compilation Error
                           ^
1 error





0 comments:

Post a Comment