Final key word in Java programming language

final is a Java keyword which is used to restrict the user, the final keyword can be used in variable, method and class.

When the variable is defined as final, you can not change the value of the final variable, so it is constant.

Usage for Java final keyword 

  • Stop value change of variable
  • Stop Method overriding
  • Stop Inheritance

Java final keyword with variable

 

We assign any variable as final it means youcan not any changes the value of the final variable (it will be constant)

This is the example for usage of final keywordin Java language

program 1

public class FinalDemo
{
public static void main (String args[])
{
final int i;                // Final variable
i=5;                       // This variable assignment can not  changed
System.out.println(i);

}                            // End of method

}                            // End of class

Output = 5


program 2

public class FinalDemo
{
public static void main (String args[])
{
final int i;              //final variable
i=5;                     //Assign the value to final variable
i++;                                          //can not be change or reassign
System.out.println(i);

}

}

There is a final variable int I; we have already assigned value as 5. We are going to change the value of this variable, but it can’t be changed

final key word  use with method

Program 1

This is method overriding program in inheritance. Two methods, Both methods holds the same name. so one method can be overridden another method.

public class FinalDemo1
{
public static void main (String args[])
{
B obj=new B();
obj.show();
}
}

class A
{
public void show()
{
System.out.println(“Show my name karmehavannan”);
}
}

class B extends A
{
public void show()
{
System.out.println(“Show my father name sathasivam”);
}
}

Program 2

This is method overriding program in inheritance. Two methods, Both methods are the same name. But the final keyword used the first method.
So both methods cannot be overridden

public class FinalDemo1
{
public static void main (String args[])
{
B obj=new B();
obj.show();
}
}
class A
{
final public void show()              //final keyword is used to this method.So it can not be overridden
{
System.out.println(“Show my name karmehavannan”);
}
}
class B extends A
{
public void show()
{
System.out.println(“Show my father name sathasivam”);
}
}

final key word use with class

if you make any class as final, you cannot extend it.

Program 1

public class FinalDemo1
{
public static void main (String args[])
{
B obj=new B();
obj.show();
}
}
class A
{
public void show()
{
System.out.println(“Show my name karmehavannan”);
}
}
class B extends A
{
public void show()
{
System.out.println(“Show my father name sathasivam”);
}
}

Program 2

public class FinalDemo1
{
public static void main (String args[])
{
B obj=new B();
obj.show();
}
}
final class A // use final key word this class
{
public void show()
{
System.out.println(“Show my name karmehavannan”);
}
}
class B extends A
{
public void show()
{
System.out.println(“Show my father name sathasivam”);
}
}

 

the final method is inherited but it cannot be override

Final class in Java

Related post

This keyword in Java

Super keyword in Java

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.