Recent

13.final keyword in Java

final Keyword In Java

final is a keyword in Java used for many purposes. One main use of final keyword is to make the variables as constant. this means we cannot change their values once defined final.

We can define 4 things as final in Java:

·         Class

·         Function

·         Data member

·         Local variable

final class:

If we define any class in Java then it cannot be inherited. So, if you want to stop inheritance then make the class as final.

For Example:

finalclassBase  //class defined final

{

voidshow()

{

System.out.println("Base class");

}

}

 

classChildextendsBase

{

publicstaticvoidmain(String[]args)

{

 

}

}

Output:

In this program, we are getting compilation error because we are trying to inherit a final class.

 

final function:

If we make any member function as final then it cannot be overridden that means we cannot override them in child class.

For Example:

classBase

{

 

finalvoidshow()  //show function is defined final

{

System.out.println("Base class");

}

 

}

 

classChildextendsBase

{

 

voidshow()

{

 

}

 

publicstaticvoidmain(String[]args)

{

 

}

}

Output:

In this program we are getting compilation error because we are trying to override the final method in Child class.

 

final Local variables: 

If we make any local variable as a final then it will become constant that means we cannot change the value of this variable throughout the class. Also we cannot assign a reference variable to another if we make it as final.

For Example:

classChild

{

 

publicstaticvoidmain(String[]args)

{

finalintx=10;

x=x+10;//this line will cause error as we

        //cannot change a final variable

System.out.println(x);

}

}

Output:

 

final static variable:

·         Similarly if we make any static data member of a class as a final then it will also become constant that means we cannot change the value of this variable throughout the class.

·         It has to be initialized at class level else it will give compilation error.

 

final non-static data member:

If we make any non static data member of a class as a final then it will also become constant that means we cannot change the value of this variable throughout the class.

·         Now the question arise that if non static and static data member both are having same functionality after we define them as final then why we are using non static data member instead we can use static member directly as it gets memory in class info area and can be accessed by name directly from the class. It was kind of memory loss for the programmer.

·         Therefore programmers gave a new functionality to this non static data member that we can define these data members as final at class level but we will give its value later whenever an object is created and after it gets its first value it will become constant.

·         But to use this feature of non static final data member, we have to make it as blank final.

Question: What is blank final data member?

Answer: A blank final data member is one which can be initialized once in the life cycle of the class but it should be initialized dynamically(via constructor).

Question: How can we make any non static data member blank final?

Answer: If we want to make any non static data member of a class as a blank final variable then it has to be initialized via parameterized constructor only.

An Example of blank final variable:

classChild

{

finalintx;  //non static variable defined final

 

//initializing via constructor that means

//we made it blank final

Child(intx)  

{

this.x=x;

}

 

publicstaticvoidmain(String[]args)

{

Childt1=newChild(10);

System.out.println(t1.x);

 

Childt2=newChild(30);

System.out.println(t2.x);

}}

Output:

No comments:

Post a Comment

Bug-dbug Designed by Bugdbug - Developed by Belson Raja Copyright © 2016

Theme images by Bim. Powered by Blogger.
Published By Bugdbug India