Java Final Keyword
In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be:
- variable
- method
- class
A final variable with no value is known as a blank final variable or an uninitialized final variable. The variable of the program is also using the java final keyword that is final can be Implemented using the variables of the written code. Only in the function Object () {[native code]} is it possible to initialize it. The final blank variable may also be static, and it will only be initialized in the static block. We shall learn about these in detail. Learn the fundamentals of the final keyword first. The final keyword is a non-access modifier that prevents changes to classes, attributes, and methods (impossible to inherit or override).
When you want a variable, such as PI, to always store the same value, the final keyword is helpful (3.14159...). The phrase "modifier" refers to the last term.
Final variable in Java:
You cannot alter the value of a final variable once it has been set (It will be constant).
A final variable example:
We are going to update the value of the final variable speed limit; however it cannot be changed because final variables that have been given a value cannot ever be changed.
class Bicycle {
final double spd_lmt=90.0;//final variable
void milage () {
spd_lmt=300;
}
public static void main (String s []) {
Bicycle b=new Bicycle ();
b. milage ();
}
}//class is ended
Output:
Error due to compile time….
Java Final Method Concept
If is implemented to any method, you cannot override it, or we cannot make changes in the program as it is stored in static manner.
Example of final method:
class Bicycle {
final void milage ()
{
System.out.println("running milage ");
}
}
class suzuki extends Bicycle {
void milage ()
{
System.out.println("300kmph is the ruuning safe limit");
}
public static void main (String s []) {
Suzukik= new Suzuki ();
k. milage ();
}
}
Output:
Error because of compile time
Java Final Class
If you make any class as final, you cannot extend it because the class or method we declare is statically imported using final keyword.
Example of final class:
final class Bicycle {}
class Suzuki1 extends Bicycle {
void milage ()
{
System.out.println(" 300kmph is the speed limit of the bicycle");
}
public static void main (String s []) {
Suzuki1 S= new Suzuki1();
S. milage ();
}
}
Output:
Error due to compiling time.
Final Method Inherited Example
The inheritance can be occurred using the final method which is cannot be override in the program,
For Example:
class Bicycle {
final void milage ()
{
System.out.println("running milage...");
}
}
class Suzuki2 extends Bicycle {
public static void main (String s[]) {
new Suzuki2(). milage ();
}
}
Output:
Running mialge...
Initialize Blank Final Variable
Only in constructor we can initialize the final variable with empty declaration that is we declare nothing in the variable.
For example:
class Suzuki10{
final int spd_lmt;
Bicycle10(){
speedlmt=140;
System.out.println(speedlimit of the bicycle is given);
}
public static void main (String s[])
{
new Bicycle10();
}
}
Output:
140
Static Blank Final Variable
A static final variable is a keyword which stores the input value permanently and it is not initialized at the time of declaration which is known as static blank final variable. It can be initialized only in static block.
Example of static blank final variable:
Class Dattu {
static final int info;
static{info=150;}
public static void main (String s []) {
System.out.println(Dattu.info);
}
}
We cannot change the value of variable ones the parameters are declared in the variable using final keyword.
class Bicycle11{
int tube (final int d) {
d=d+2;//the value of the d cannot be changed as it is declared with the final keyword
d*d*d;
}
public static void main (String s []) {
Bicycle11 b=new Bicycle11();
b. tube (5);
}
}
Output:
Due to compiling time Error is caused.