Master java skills

Final keyword

In general terms, final keyword makes things unchangeable. But it can be used in different context in java. It can be applied on following things

  1. Variables
  2. Method
  3. Class

Final variables

If we make a variable final, then it’s value cannot be changed. It becomes a constant. We cannot assign any other value to that variable.

final int num = 10;
final String str = "Rahul";

Let’s see one example

package com.javatrainingschool;

public class FinalVariableExample {

	private final int var = 10;
	private final String str = "Rahul";
	
	public static void main(String[] args) {
		
		FinalVariableExample obj = new FinalVariableExample();
		System.out.println("Value of var = " + obj.var);
		
		obj.var = 20;  //compiler error
	}
}

In the above example, we will get compiler error – “The final field FinalVariableExample.var cannot be assigned

Initialization of final variables inside constructor

Final variables are initialized right at the time of declaration. But, there is another way of initializing them and that is inside the constructor. So, if you choose a final variable not to initialize at the time of declaration, then it must be initialized inside the constructor. See the below example

package com.javatrainingschool;

public class FinalVariableExample {

	private final int var;
	private final String str;
	
	public FinalVariableExample(int var, String str) {
		super();
		this.var = var;
		this.str = str;
	}

	public static void main(String[] args) {
		
		FinalVariableExample obj = new FinalVariableExample(20, "abc");
		System.out.println("Value of var = " + obj.var);
		
	}
}

Final Instance and static variables

So far, we have seen final variables which are instance variables of the class. It means, the value of variables are constant only for that object. For another object, these values can be different and constant.

Similary, we can apply final on static variables also. In those cases, it becomes constant for all the objects. That’s why when we want to make a value constant for a class, we should also make it static. In that case, it would be constant in real sense. Let’s see one example where the constant is static attribute of the class

package com.javatrainingschool;

public class FinalVariableExample {

	private final int var;
	private final String str;
	public static final String VALUE = "XYZ";
	
	public FinalVariableExample(int var, String str) {
		super();
		this.var = var;
		this.str = str;
	}

	public static void main(String[] args) {
		
		FinalVariableExample obj = new FinalVariableExample(20, "abc");
		System.out.println("Value of var = " + obj.var);
		
		System.out.println("Value of static field" + VALUE);
		
	}
}

Naming convention for constants

Constants should be decared in all capitals and underscore to separate the words. Such as VALUE, NUMBER, EMPLOYEE_NAME.

Final methods

If we make a method final, then it cannot be overridden. Its definition becomes final.

Note -> Final methods cannot be overridden. However, they are inherited by the subclasses.

package com.javatrainingschool;

public class Parent {

	public final void show() {
		System.out.println("From inside show method.");
	}
	
}
package com.javatrainingschool;

public class Child extends Parent {

	public void show() {     //compiler error (Cannot override the final method from Parent)
		System.out.println("From inside Child show method.");
	}
	
}

Final class

If we make a class final, then that class cannot be extended. It means, there cannot be subclasses of this class.

package com.javatrainingschool;

public final class Parent {

	public void show() {
		System.out.println("From inside show method.");
	}
	
}
package com.javatrainingschool;

public class Child extends Parent {     //compiler error - (The type Child cannot subclass the final             class Parent)
              

	public void show() {
		System.out.println("From inside Child show method.");
	}
	
}

Summary

  1. Final variables becomes constant
  2. Final methods cannot be overridden
  3. Final classes cannot be extended/subclassed
  4. Constructors cannot be final