Powered by Blogger.

The Final Keyword in Java Program- Final variable, Method and Class

The Final Keyword:-

In the Java technology, the final keyword can be apply to the classes, methods, and variables. In Java, final classes can not be inherited, final methods can not be override, and final variables are constant. Any attempt to change the value of a final variable causes a complier error. A blank final variable is a final variable that is not initialized in its declaration, but it can be initialized later once only.

The final Keyword Example:-

 public final class TestFinal{
   public final int studentID = 10;

   public final void diplay(){
      System.out.println(”Final method can not be override”);
   }
 }

Java Final Keyword:-

1. A java variable can be declared using the keyword final. Then the final variable can be assigned only once.

2. A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialise it.

3. Java classes declared as final cannot be extended. Restricting inheritance!

4. Methods declared as final cannot be overridden. In methods private is equal to final, but in variables it is not.

5. final parameters – values of the parameters cannot be changed after initialization. Do a small java exercise to find out the implications of final parameters in method overriding.

6. Java local classes can only reference local variables and parameters that are declared as final.

7. A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance.

A Discussion Inviting Controversy on Java Final Keyword:-

‘final’ should not be called as constants. Because when an array is declared as final, the state of the object stored in the array can be modified. You need to make it immutable in order not to allow modifcations. In general context constants will not allow to modify. In C++, an array declared as const will not allow the above scenario but java allows. So java’s final is not the general constant used across in computer languages.

A variable that is declared static final is closer to constants in general software terminology. You must instantiate the variable when you declare it static final.

Definition as per java language specification (third edition) – 4.12.4 is “A final variable may only be assigned to once.”(§4.1.2)

Java language specification tries to redefine the meaning of constant in the following way!

We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).

Final Classes:-

You will notice that a number of the classes in Java library are declared final, e.g.

public final class String

This means this class will not be subclassed, and informs the compiler that it can perform certain optimizations it otherwise could not. It also provides some benefit in regard to security and thread safety.

The compiler will not let you subclass any class that is declared final. You probably won't want or need to declare your own classes final though.

Final Methods:-

You can also declare that methods are final. A method that is declared final cannot be overridden in a subclass. The syntax is simple, just put the keyword final after the access specifier and before the return type like this:

public final String convertCurrency()

Final Fields:-

You may also declare fields to be final. This is not the same thing as declaring a method or class to be final. When a field is declared final, it is a constant which will not and cannot change. It can be set once (for instance when the object is constructed, but it cannot be changed after that.) Attempts to change it will generate either a compile-time error or an exception (depending on how sneaky the attempt is).

Fields that are both final, static, and public are effectively named constants. For instance a physics program might define Physics.c, the speed of light as

public class Physics {

  public static final double c = 2.998E8;

}

In the SlowCar class, the speedLimit field is likely to be both final and static though it's private.

public class SlowCar extends Car {

  private final static double speedLimit = 112.65408; // kph == 70 mph

  public SlowCar(String licensePlate, double speed, double maxSpeed,
   String make, String model, int year, int numberOfPassengers, int numDoors) {
    super(licensePlate,
     (speed < speedLimit) ? speed : speedLimit,
     maxSpeed, make, model, year, numberOfPassengers, numDoors);
  }

  public void accelerate(double deltaV) {

     double speed = this.speed + deltaV;
    
     if (speed > this.maxSpeed) {
       speed = this.maxSpeed;
     }
    
     if (speed > speedLimit) {
       speed = speedLimit;
     }
    
     if (speed < 0.0) {
       speed = 0.0;
     }
    
     this.speed = speed;   
    
  }
  
}

Final Arguments:-

Finally, you can declare that method arguments are final. This means that the method will not directly change them. Since all arguments are passed by value, this isn't absolutely required, but it's occasionally helpful.

What can be declared final in the Car and MotorVehicle classes?

2 comments:

  1. I really enjoy the blog.Much thanks again. Really Great.
    Very informative article post. Really looking forward to read more. Will read on…


    oracle online training
    sap fico online training
    dotnet online training
    qa-qtp-software-testing-training-tutorial

    ReplyDelete
  2. I appreciate you sharing this article. Really thank you! Much obliged.
    This is one awesome blog article. Much thanks again.

    sap online training
    software online training
    sap sd online training
    hadoop online training
    sap-crm-online-training

    ReplyDelete