Prev Next

Java / final keyword

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. What is the use of the final keyword in Java?

The final keyword can be used with a class, method, and variables.

With class, it prevents inheritance by not allowing to create subclasses.

With methods, it prevents overriding, you cannot override a final method in Java.

With variable, it is treated as constant because you cannot change their value once assigned.

2. Can we make an array final in Java? Can you change its elements after initializing?

Yes, you can make an array final in Java and you can change it's elements as well. Both array and collection classes can be made final and you can still change their elements.

3. Difference between abstract method and final method in Java.

The abstract method is incomplete while the final method is regarded as complete. The only way to use an abstract method is by overriding it, but you cannot override a final method in Java.

4. Can you overload a final method in Java?

Yes, you can overload a final method in Java.

5. Can a static method be final in Java?

Yes, you can make a static method final in Java.

When we declare a static method as final its prevents from method hiding and encounters compile time error: Cannot override the final method from Parent.

public class Parent {

	final static void helloWorld() {

		System.out.println("Hello from Parent.");
	}

}

class Child extends Parent {

	// compile error : Cannot override the final method from Parent
	static void helloWorld() {

		System.out.println("Hello from child.");
	}

}
6. Can a class be abstract and final?

No. It is not possible.

7. Can a method be abstract and final?

No, not possible. final implies that the method is complete and cannot be overridden whereas abstract marks an incomplete method and needs to be overridden.

8. Can you override a final method in Java?

No, you cannot override a final in Java.

9. Where to initialize a non-static final variable?

Non-static final variable can be initialized when declared or in constructor only. Otherwise we will encounter compilation error.

10. Where to initialize a static final variable in Java?

Static final variable can be initialized where it is declared or using static block.

11. Define a Blank final variable.

A final variable declared but not assigned is known as a blank final variable. It can be initialized within a constructor only. It raises a compilation error if it is not initialized.

12. What is Static blank final variable?

It is a blank final variable declared as static. That is, a final static variable declared but not given a value or not initialized is known as a static blank final variable. It can be initialized through a static block only.

13. What is Java finally block?

Java finally block is used to execute important code such as closing connection, stream etc.

It is always executed whether exception is handled or not. Java finally block follows try or catch block.

«
»
static keyword

Comments & Discussions