0% found this document useful (0 votes)
77 views

Final Keyword in Java

The final keyword in Java can be applied to classes, methods, and variables. It restricts changes to final entities. Final variables cannot be reassigned, final methods cannot be overridden, and final classes cannot be subclassed. The final keyword requires initialization of variables before use to make their values constant and immutable.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Final Keyword in Java

The final keyword in Java can be applied to classes, methods, and variables. It restricts changes to final entities. Final variables cannot be reassigned, final methods cannot be overridden, and final classes cannot be subclassed. The final keyword requires initialization of variables before use to make their values constant and immutable.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Final Keyword in Java

The final keyword in java is used to restrict the user. The java final keyword can be
used in many context. Final can be:

Final is the modifier applicable for classes, methods and variables.

1) Java final variable:-

If you make any variable as final, you cannot change the value of final variable(It
will be constant).

Ex:-

class Bike{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike obj=new Bike();
obj.run();
}
}//end of class
Output: Compile Time Error.
2) Java final method

 If you make any method as final, you cannot override it.


 Whatever the methods parent has by default available to the child.
 If the child is not allowed to override any method, that method we have to
declare with final in parent class. That is final methods cannot overridden.

Example:
Program 1:
class Parent{
public void property(){
System.out.println("cash+gold+land");
}
public final void marriage(){
System.out.println("subbalakshmi");
}}
Program 2:
class child extends Parent{
public void marriage(){
System.out.println("Pooja");
}}
OUTPUT:
Compile time error.
D:\Java>javac Parent.java
D:\Java>javac child.java
child.java:3: marriage() in child cannot override marriage() in Parent; overridden
method is final public void marriage(){
Ex-2:-
class Bike{
final void run(){
System.out.println("running");
}
}

class Honda extends Bike{


void run(){
System.out.println("running safely with 100kmph");
}

public static void main(String args[]){


Honda honda= new Honda();
honda.run();
}
}
Output:Compile Time Error

3) Java final class:-


 If you make any class as final, you cannot extend it.
 If a class declared as the final then we cann't creates the child class that is
inheritance concept is not applicable for final classes.

Example:

Program 1:

final class Parent

Program 2:

class child extends Parent

OUTPUT:

Compile time error.

D:\Java>javac Parent.java

D:\Java>javac child.java

child.java:1: cannot inherit from final Parent class child extends Parent

Ex-2:-
final class Bike{

class Honda1 extends Bike{

void run(){

System.out.println("running safely with 100kmph");

public static void main(String args[]){

Honda1 honda= new Honda1();

honda.run();

Output: Compile Time Error

Note: Every method present inside a final class is always final by default whether
we are declaring or not. But every variable present inside a final class need not be
final.

Example:

final class parent{

static int x=10;

static

{
x=999;

}}

The main advantage of final keyword is we can achieve security.

Whereas the main disadvantage is we are missing the key benefits of oops:

polymorsim (because of final methods), inheritance (because of final classes)


hence if there is no specific requirement never recommended to use final keyboard.

Q) Is final method inherited?

Ans) Yes, final method is inherited but you cannot override it. For Example:

class Bike{

final void run(){

System.out.println("running...");

class Honda2 extends Bike{

public static void main(String args[]){

new Honda2().run();

Output:running...

Final instance variables:


 If the value of a variable is varied from object to object such type of
variables are called instance variables.
 For every object a separate copy of instance variables will be created.

For the instance variables it is not required to perform initialization explicitly jvm
will always provide default values.

Example:

class Test{

int i;

public static void main(String args[]){

Test t=new Test();

System.out.println(t.i);

}}

Output:

D:\Java>javac Test.java

D:\Java>java Test

0
If the instance variable declared as the final compulsory we should perform
initialization explicitly and JVM won't provide any default values. Whether we are
using or not otherwise we will get compile time error.

Example:

Program 1:

class Test{

int i;

Output:

D:\Java>javac Test.java

D:\Java>

Program 2:

class Test{

final int i;

Output:

Compile time error.

D:\Java>javac Test.java

Test.java:1: variable i might not have been initialized class Test

Rule:
For the final instance variables we should perform initialization before constructor
completion. That is the following are various possible places for this.

1) At the time of declaration:

Example:

class Test{

final int i=10;

Output:

D:\Java>javac Test.java

D:\Java>

2) Inside instance block:

Example:

class Test{

final int i;

i=10;

}}

Output:

D:\Java>javac Test.java

D:\Java>
3) Inside constructor:

Example:

class Test{

final int i;

Test(){

i=10;

}}

Output:

D:\Java>javac Test.java

D:\Java>

If we are performing initialization anywhere else we will get compile time error.

Example:

class Test{

final int i;

public void m1(){

i=10;

}}

Output:

Compile time error.

D:\Java>javac Test.java
Test.java:5: cannot assign a value to final variable i

i=10;

Final static variables:

 If the value of a variable is not varied from object to object such type of
variables is not recommended to declare as the instance variables. We have
to declare those variables at class level by using static modifier.
 In the case of instance variables for every object a separate copy will be
created but in the case of static variables a single copy will be created at
class level and shared by every object of that class.
 For the static variables it is not required to perform initialization explicitly
jvm will always provide default values.

Example:

class Test{

static int i;

public static void main(String args[]){

System.out.println("value of i is :"+i);

}}

Output:

D:\Java>javac Test.java

D:\Java>java Test

Value of i is: 0
If the static variable declare as final then compulsory we should perform
initialization explicitly whether we are using or not otherwise we will get compile
time error.(The JVM won't provide any default values).

Rule:

For the final static variables we should perform initialization before class loading
completion otherwise we will get compile time error. That is the following are
possible places.

1) At the time of declaration:

Example:

class Test {

final static int i=10;

Output:

D:\Java>javac Test.java

D:\Java>
2) Inside static block:

Example:

class Test{

final static int i;

static{

i=10;

}}

Output:

Compile successfully.

If we are performing initialization anywhere else we will get compile time error.

Example:

class Test{

final static int i;

public static void main(String args[]){

i=10;

}}

Output:

Compile time error.

D:\Java>javac Test.java

Test.java:5: cannot assign a value to final variable i


i=10;

Final local variables:

 To meet temporary requirement of the Programmer sometime we can


declare the variable inside a method or block or constructor such type of
variables are called local variables.
 For the local variables jvm won't provide any default value compulsory we
should perform initialization explicitly before using that variable.

Example:

class Test {

public static void main(String args[]){

int i;

System.out.println("hello");

}}

Output:

D:\Java>javac Test.java

D:\Java>java Test

Hello

Example:

class Test{

public static void main(String args[]){

int i;
System.out.println(i);

}}

Output:

Compile time error.

D:\Java>javac Test.java

Test.java:5: variable i might not have been initialized

System.out.println(i);

Even though local variable declared as the final before using only we should
perform initialization.

Example:

class Test{

public static void main(String args[]){

final int i;

System.out.println("hello");

}}

Output:

D:\Java>javac Test.java

D:\Java>java Test

hello

Note: The only applicable modifier for local variables is final if we are using any
other modifier we will get compile time error.

You might also like