With the Java final keyword we define our variable (primitive type value or reference value) to be non changeable once assigned.
Why it is needed and why you should use it will be explained in this tutorial. Go on reading
Note: Every code was written and tested in Eclipse IDE and this tutorial was made based on codes and outputs from Eclipse IDE, we suggest that you also use Eclipse IDE, for easier following of the codes and tutorial.
The Java final keyword
The use of final keyword in Java is in different contexts. But firstly we need to say that it is non-access modifier, accessible only to a variable, a method, or a class. The Java final keyword allows us to set limitations on extensiveness for various reasons.
The Java final class
Classes marked as final cannot be extended. it is used like this:
final class Example {
// your code goes here
}
Here we made an example just how to create a final class. If we declare a class as final, than all of its methods are implicitly declared as final. Here is one example of using Java final class.
package com.xenovation;
/**
* your final class.
*/ final class Hello {
void greetings() {
System.out.println("Hello World");
}
}
/**
* your main class to run this code.
*/
public class FinalClassExample {
public static void main(String[] args) {
// create an object
Hello obj = new Hello();
// calling method
obj.greetings();
}
}
So let's explain this code, if we run the program as it is we will get an output as "Hello World" and there will be no problems or suggestions.
And now with the final class the same example (which fails to compile):
package com.xenovation;
/**
* your final class.
*/
final class Hello {
void greetings() {
System.out.println("Hello World");
}
}
/**
* your main class to run this code.
*/ public class FinalClassExample { public static void main(String[] args) {
// create an object
Hello obj = new Hello();
// calling method
obj.greetings();
}
}
/**
* the example to extend a final class, which FAILS!.
*/ class FinalExtendsExample extends Hello {
void greet() {
System.out.println("Hello extended World");
}
}
Here your IDE (Eclipse) will report you a error and suggest you to "Remove 'final' modifier of 'Hello' ". If you navigate in Eclipse to "Problems" tab you will see that there is one error and it says "The type FinalExtendsExample cannot subclass the final class Hello"
The Java final variable
Considering Java final variable, variables that are marked as final cannot be reassigned. Once a final variable is initialized, it can't be altered with.
Firstly we have to show how to initialize them. In next example we will show how different ways of initializing final variables.
package com.xenovation;
class ExampleOfInitializations {
final int DIRECT = 5;
final int BLANK;
{
BLANK = 25;
}
final int DifferentBlank;
public ExampleOfInitializations() {
DifferentBlank = -1;
}
static final double StaticPI = 3.141592653589793;
static final double StaticBlank;
static {
StaticBlank = 2.3;
}
}
Now that we explained how to use them, we need to explain when to use them. That can be explained by understanding the difference between normal and final variable. Basically the difference is that we can re-assign value to a normal variable, but we cannot do that for a final variable. That explains that we should use final variables only when we want values to remain constant during the program execution.
The Java final method
When a method is declared with final keyword, it is called a final method. This kind of method cannot be overridden or hidden by sub-classes. Final method is used to prevent unexpected behavior from a sub-class altering a method.
package com.xenovation;
class Methods {
public void m1() {
System.out.println("A");
}
public final void m2() {
System.out.println("B");
}
public static void m3() {
System.out.println("C");
}
public static final void m4() {
System.out.println("D");
}
}
class Testing extends Methods {
public void m1() {
System.out.println("E");
} // OK, overriding Base#m1()
public void m2() {
System.out.println("F");
} // forbidden
public static void m3() {
System.out.println("G");
} // OK, hiding Base#m3()
public static void m4() {
System.out.println("H");
} // forbidden
}
In this example we have created 4 methods in class "Methods" and 4 methods in class "Testing" that extends class "Methods". If you copy and paste this program you will get 2 errors/suggestions. Both of them are in class "Testing" and they are method "m2" and method "m4". The reason why we have errors/suggestions is because we tried to extend final methods. That is not possible, we can extend normal methods but final methods has to remain as declared.
To find out more difference between Java final or Java static go on here