21) Final Keyword

Final:

  1. Is a keyword.

  2. The final keyword can be used in many contexts.

final can be:

Variable

Method

Class

  1. If you want to declare constant variables where in the value cannot change, then those variables should be declared with the keyword “final”.

  2. Final variable value cannot be changed or overridden.

  3. Both local and global variable can be final.

  4. Global final variable should be initialized at the time of initialization itself.

  5. Local final variable can be declared once and initialized later.

  6. In terms of inheritance, final keyword us used to avoid overriding.

  7. Final class cannot be inherited.

  8. Final class methods cannot be overridden because for method overriding inheritance is mandatory.

Example:

package com.gs.ilp.corejava.finalKeyword;

public class A {
	public static void main(String[] args) {
		final int i = 10;
		i = 20;
		System.out.println(i);
	}
}

Output:

Note:

  • If you make a variable as final then we can never re-initialize that variable.

  • Static and non-static variable if made Final then its initialization is mandatory or else we will get blank field error.

Example:

package com.gs.ilp.corejava.finalKeyword;

public class B {
	final int j;// error because not initialized

	public static void main(String[] args) {
		final int j;// local variables should be initialized before usage System.out.println(j);
	}
}

Output:

Exception in thread "main" java.lang.Error: Unresolved compilationproblem: The local variable j may not have been initialized

Note:

In case of local variable make it final there is no error, but using it without initialization will throw error.

Example:

package com.gs.ilp.corejava.finalKeyword;

public class E {
	public void test(final int i) {
		i = 30;// The final local variable i cannot be assigned. It must be blank and not using
				// a compound assignment
		System.out.println(i);
	}

	public static void main(String[] args) {
		E e1 = new E();
		e1.test(10);
	}
}

Output:

The final local variable i cannot be assigned. It must be blank and not using a compound assignment

Note: If you make an array as final then its size cannot be altered.

Example:

package com.gs.ilp.corejava.finalKeyword;

public class F {
	public static void main(String[] args) {
		final int[] a = new int[5];
		a[0] = 10;
		a[0] = 20;
		System.out.println(a[0]); // 20
	}
}

Output:

20

Example:

package com.gs.ilp.corejava.finalKeyword;

public class G {
	public static void main(String[] args) {
		final int[] a = new int[5];
		a = new int[2];
	}
}

Output:

Note:

  1. When array is final, its size cannot be changed.

  2. When array is not final, then size can be altered.

  3. When (String[] args) is final, then its size cannot be altered. args[] is a take string no number allowed

Example:

package com.gs.ilp.corejava.finalKeyword;

public class H {
	public static void main(String[] args) {
		args = new String[3];
		args[0] = "10";
		System.out.println(args[0]);
	}
}

Output:

10

Note:

  1. Inheritance of final class is not possible.

  2. Final Variable value cannot be altered.

  3. Final Array size cannot be altered, but value can be altered.

  4. Final Class, inheritance is not possible and value cannot be inherited.

Example:

package com.gs.ilp.corejava.finalKeyword;

final class J {
	int i = 10;
}

final class I extends J // the type I cannot sub class to final
{
	public static void main(String[] args) {
		J j1 = new J();
		System.out.println(j1.i);
	}
}

Output:

Unresolved compilation problem the type I cannot sub class to final

Note: A final class can inherit the members of non-final class.

Example:

package com.gs.ilp.corejava.finalKeyword;

class L {
	int i = 20;
}

final class K extends L {
	public static void main(String[] args) {
		L l1 = new L();
		System.out.println(l1.i);
	}
}

Output:

20

Note:

If you make method as final, meaning that sub classes cannot override this method. The compiler checks and gives an error if you try to override the method.

When we want to restrict overriding then make a method as a final.

Example:

package com.gs.ilp.corejava.finalKeyword;

class XYZ {
	final void demo() {
		System.out.println("XYZ Class Method");
	}
}

public class ABC extends XYZ {
	void demo() {
		System.out.println("ABC Class Method");
	}

	public static void main(String args[]) {
		ABC obj = new ABC();
		obj.demo();
	}
}

Output:

The above program would throw a compilation error as we cannot override a final method.

Last updated