Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Java Final Keyword

In this article, we will focus on understanding the meaning of an important keyword, the final keyword that is provided in Java.

The idea of keyword final is used to put in some restrictions in the program for the user or say the developers.

We divided this tutorial in the following parts:

  • Understanding the meaning of final
  • What all can be declared as final
  • Explanation of final variable
  • Explanation of final method
  • Explanation of final class

Understand the Meaning of final

In a program at times, we are required to put in some restrictions for the programmers. These restrictions are there to prohibit the users from doing some particular kind of modifications and development in the program.

The keyword final is used to put in these restrictions in a java based application.

The restrictions that can be put in by using the final keyword are mainly into three categories: restricting the class from inheriting it further, restricting the method from being overridden further and restricting the variable from being changed further.

In further sections we will understand that how final is used in different ways to put in these restrictions.

What all can be declared as final

The main three artefacts in java those can be declared as final are:

  • A variable can be declared as final
  • A method can be declared as final
  • A class can also be declared as final

Let us discuss each of these now in detail with their meaning, need and importance.

The final variable

A variable, the value of which we do not want to get changed in the whole program is usually called as a constant. Such variables are declared as final. That means to declare constants in a java program we must declare the concerned variable with the keyword final.

When there is a need of a constant value that should not be accidently changed further then we must decorate the declaration of such variable with the keyword final.

Once we have declared a variable as final, that means we can initialize the variable at once only with some valid value. But once the variable is initialized and assigned a value in the program, then further you cannot make any change in the value of the variable declared as variable.

It is better that we understand it with the help of an example.

For example, we need to do write a mathematical program, where we would be using an important mathematical constant PI symbolically written as П whose value is constant that is 3.14.

So we must declare a variable say PI of type float. We must assign it value 3.14. But once assigned the value must not be changed further in the program. We must ensure so in the program while declaring PI. So theoretically PI is a variable that will be constant.

In the following program we declare the PI as constant and stop any change in the program other than the place where it is initialized initially.

class calculation {	….	….
	float static final PI = 3.14
	….
	….
	public static void main (String args []) {
	…..
	float area = PI*r*r;
	PI = 1.23; // COMPILER ERROR
	…..
	}
	}

In the program above, PI is declared and initialized with valid value 3.14 at line 4. We use the PI in the code at line 9 to compute the value of area. But in the code at line 10, we are trying to change the value of the PI. Now at this place the compiler will through an error and the code will not compile.

The reason for error at line 10 is because of the fact that PI is declared as final in code at line 4 in the program. With declaring it as final, we can assign a value to it initially only once that we are doing at line 4 itself. But further any changes in the value of the PI are restricted in the program.

Therefore, even if accidently the a programmer ever tries to change the value of PI, the compiler will restrict the changes at compile time and will not let the program compile successfully.

The final method

Next thing that can be declared as final in a java program is a method of a class.

Once a method is declared asfinal that means the method cannot be overridden in the derived class.

We define a method in a base class. We provide the implementation to the method as required. Now we create a derived class from this base class. We can redefine the base class method in the derived class.This phenomenon is called as method overriding. What if, we do not want the implementation of the method to be changed in the derived classes? We want all the derived classes to have the same implementation as defined in the base class. That simply means we need to stop overriding of the said method.

One way is to tell all the developers about it that this method should not be overridden in the derived classes. So the developers will take care. Also, you can provide the same in the documentation of the class itself. What if later a new developer joins in and is not aware of the fact about this method. In such a case developer may accidently override this method and provide an implementation of the method. This all may then change the behaviour of the application which is not desired.

Important question is how to put this information in the application, so that anyone who further tries write such a code is warned by the code itself. The idea is to put restrictions at the compiler level only.

The final keyword is used to put this restriction and notify the complier.

We can declare a method as final like below:

final return-type method-name (arguments information) {
...
// method code here
}

final void initializeapplication (String str) {
...........
}

Here, initializeapplication method is declared as final.

Let us see a complete class code.

class Test {
final void initializeapplication (String str) {
...........
...........
}
}
class TestDerived extends Test{
// Override the method initializeapplication
// that is defined in the base class Test
	void initializeapplication (String str){
......................
	}
	}

We will now compile the Test and TestDerived classes. The Test class will compile successfully without any error. Next, we compile the TestDerived class. In this case, the compiler will give the error. The error is due to the code written at line number 2. Here we are overriding the initializeapplication method in TestDerived class. The TestDerived class inherits this method form the Test class. But note that in the Test class the method initializeapplicationis declared as final. Hence the compiler does not allow the further overriding of this method inany of the subclasses of the Test class.. When we try to do so in the TestDerived class, we get an error by the compiler itself.

So with the help of the final declaration, we have imposed a restriction the application. This will stop any one further modifying the method initializeapplication in the application.

The final class

Next and the last thing that can be declared as final in a java program is a class.

When we declare a class as final that means the same class cannot be inherited further. We cannot create the subclasses or derived classes of a final class in a java program.

At times in applications, we do not want classes to be further sub classed. We want to stop the inheritance till that class only. Then in this case, the final keyword is applied to the class.

Sometimes, we have a unique class in our application and the behaviour of the class should not be altered in the program by the use of inheritance. We all know that by the use of proper inheritance and dynamic polymorphism, we can easily change the behaviour of the object at runtime. What if we do not want the same thing for our class?

In this case, we are must put in a restriction at the compile level. We must declare the class in a way, such that the compiler itself does not allow the further derived classes of our class. And if any attempt is made to do so, proper errors are shown.

The way to put such a restriction is to simply declare the class as final.

The syntax for declaring a class as final is:

final class <class-name> {
class data-member
class methods
....................
}

Let us take a brief and very simple example now. We declare a class Test with minimal fucntionality. The class is declared using the final keyword.

	final class Test {
	inti;
	// Constructor of Test with  no arguments - Default
	Test () {
	}
	}

Now try to create a new class say with name TestDerived that is derived from the Test class. We can do the same simply as following

class TestDerived extends Test {
inti;
// Constructor of Test with  no arguments - Default
Test () {
}
}

Now try to compile both the classes using a java compiler. First compile the Test class and note the results. The Test class will be compiled successfully by the java compiler without any error. Now try to compile the class TestDerived. The java compiler will give the error now. The message of the error would read like "Test is declared as final - cannot be extended".

Simply the reason is that while declaring the class Test in line number 1 above, you have used the keyword final. With the use of this keyword, the class Test is compiled as to be a final class. Further we declare the class TestDerived inherited from the Test class. So there is an error in the TestDerived. Being the class Test as final, it cannot be inherited.

We finish here our today’s article, hope you liked, let me know in the comments below if you have any question.



computer science Graduate :Ceo at www.gigsclerk.com (Best Marketplace to Buy and sell service online from 2$-100$ ) and www.trustingeeks.com. Interested in programming, online buissiness. Android development.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login