Java Class 6
Java Class 6
The final keyword can be applied with the variables, a final variable
final variable can be static also which will be initialized in the static
block only.
1) Java final variable
If you make any variable as final, you cannot change the value
of final variable(It will be constant).
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class
2) Java final method
class Bike{
final void run(){System.out.println("running");}
}
class Bike{
final void run(){System.out.println("running...");}
}
class Honda2 extends Bike{
public static void main(String args[]){
new Honda2().run();
}
}
static blank final variable
A static final variable that is not initialized at the time of declaration is
known as static blank final variable. It can be initialized only in static
block.
class A{
static final int data;//static blank final variable
static{ data=50;}
public static void main(String args[]){
System.out.println(A.data);
}
}