Monday, 1 December 2014

What is final keyword ?


final  Keyword In Java:

The final keyword in java is used to restrict the user.
The final keyword can be used in many context. final can be:
>1. variable
>2. method
>3. class.
The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these.

final variable :

If you make any variable as final you can not change the value of  that variable.

Example of final variable:

There is a final variable speed, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.

class Car{
final int speed=90; //final variable
void run(){
speed=100;
 }
public static void main(String []args){
Car obj=new Car();
obj.run();
  }
}

Output: Compile Time Error


final method :

If you make any method as final you can not override that method.

Example of final method:

class Car{
final void run()  { System.out.println("running");}
}
class Honda extends Car{
void run()  { System.out.println("running  with 50kmph");}

public static void main(String []args){
Honda h= new Honda();
h.run();
   }
}

Output: Compile Time Error

final class :

If you make any class as final you can not extends  that class.

Example of final class:

final class Honda{}

class Car extends Honda{
void run()   { System.out.println("running  with 20kmph");}

public static void main(String []args){
Car c= new Car();
c.run();
   }
}

Output: Compile Time Error



Que> What is blank final variable ?

Ans> A final variable that is not initalized at the time of declaration is known as blank final variable.
 If you want to create a variable that is initialized at the time of creating object and once initialized    may not be changed, it is useful.For example PASSPORT NO number of an employee.
 It can be initialized only in constuctor.

Example of blank final variable:

class Employee{
int id;
String name;
final String PASSPORT_NUMBER;

 }

Que) Can we intialize blank final variable ?



 Ans> Yes,but only in constructor.
 For example:

class Car{
final int speed;   //blank final variable
Car(){
speed=50;
System.out.println(speed);
}
public Static void main(String [] args){
new Car();
  }

Output: 50


Que) What is final parameter ?

If you declare any parameter as final, you cannot change the value of it.

class Test{
int square(final int x){
x=x+3;   //can't be changed as x is final
x*x*x;
}
public static void main(String []args){
Test b=new Test();
b.square(5);
  }
}

Output: Compile Time Error

Que> Can we declare a constructor as final?

Ans> No,because constructor is never inherited. we can not inherite constructor means already                     final(internally) so we can't make again as final.

Que> What is static blank final variable ?

Ans> A static final variable that is not initalized at the time of declaration is known as static blank final variable. It can be initialized only in static block.

Example of static blank final variable:

class Test{
static final int age;   //static blank final variable
static { age=20;}

public Static void main(String []args){
System.out.println(Test.age);
  }
}

Output: 20

No comments:

Post a Comment