• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Top 20 Java Interview Questions on Final keyword

April 16, 2019 by javainterviewpoint Leave a Comment

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

Final keyword can be applied to variable, method, and class. Each of them has its own uses

  • The final variable is a variable whose value cannot be changed at any time once assigned, it remains as a constant forever.
  • The final method cannot be overridden
  • A final class cannot be subclassed (cannot be extended)

2. What is a blank final variable?

A blank final variable is a final variable, which is not initialized during declaration.

Java Interview Questions on Final keyword

3. Can we declare final variable without initialization?

       Yes, We can declare a final variable without initialization and these final variables are called a blank final variable but must be initialized before usage.

The final variable can be initialized in the below four ways

1. Through Instance Initialization Block (IIB)

The Instance Initialization Block is used to initialize the instance data member, this block runs every time whenever the object of the class is created. Instance Initialization Block gets executed exactly before the code in the constructor. We can use IIB to initialize an instance final variable

public class Test
{
    // Blank final variable
    final int SPEED;
    // Instance Initialization Block
    {
        SPEED = 5;
    }
    public static void main(String args[])
    {
        Test t=new Test();
        System.out.println("Travelling Speed is :"+t.SPEED);
    }
}

2. Through Static Initialization Block

The static block is a block of code inside a Java class, which will be executed when a class is first loaded into the JVM. The Static Initialization Block can be used to initialize a class final variable/static final variable

public class Test
{
    // Blank final variable
    static final int SPEED;

    // Static Initialization Block
    static
    {
        SPEED = 10;
    }

    public static void main(String args[])
    {
        System.out.println("Travelling Speed is :" + SPEED);
    }
}

3. Through Constructor

A constructor also can be used to initialize a blank final variable.

public class Test
{
    // Blank final variable
    final int SPEED;

    // No Parameter Constructor
    Test()
    {
        SPEED = 15;
    }

    public static void main(String args[])
    {
        Test t=new Test();
        System.out.println("Travelling Speed is :"+t.SPEED);
    }
}

4. Within Method

The local final variable can be initialized during declaration or any place after the declaration. It must be initialized before used.

public class Test
{
    public static void main(String args[])
    {
        // Local final variable
        final int SPEED;

        SPEED = 25;

        System.out.println("Travelling Speed is :" + SPEED);
    }
}

4. What is a final method?

When a method is declared as final, then it is called as a final method, The subclass can call the final method of the parent class but cannot override it.

5. What is a final class?

A class declared with a final keyword is called a final class, a final class cannot be subclassed. This means a final class cannot be inherited by any class.

6. Can a main() method be declared final?

Yes, the main() method can be declared as final and cannot be overridden.

7. Can we declare constructor as final?

        No, Constructor cannot be declared as final. Constructors are not inherited and so it cannot be overridden, so there is no use to have a final constructor.

You will get an error like “Illegal modifier for the constructor in type Test; only public, protected & private are permitted”

8. Can we declare an interface as final?

       The sole purpose of Interface is to have the subclass implement it if we make it final it cannot be implemented. Only public & abstract are permitted while creating an interface

9. Can Final Variable be serialized in Java?

Yes, the final variable can be serialized in Java

10. What will happen if you add final to a List / ArrayList?

Once we created a final List/ ArrayList we can add or remove data from the list but the final list cannot be assigned with a value. Let’s look into the below code

import java.util.ArrayList;
import java.util.List;

public class FinalList
{
    public static void main(String[] args)
    {
        final List myList = new ArrayList();
        myList.add("one");
        myList.add("two");
        myList.add("three");
        
        System.out.println(myList);
        
        List tmp = new ArrayList();
        tmp.add("four");
        
        myList = tmp; // Throws Compilation error
        myList = null;
    }
}

When we assign our myList with tmp or null, we will be getting a compilation error like “The final local variable myList cannot be assigned”

11. Can we make a method final in Java?

Yes, We can make a method final, the only constraint is that it cannot be overridden.

12. What is effectively final in Java?

The term effectively final variable is introduced in Java 8. A variable which is not declared as final but the value never changed after initialization is called as effectively final.

13. Can we make the local variable be final?

       Yes, we can make a local variable final in Java. In fact, the final is the only modifier which can be applied to a local variable. If we apply any other modifier we will be getting compile time error [only final is permitted].

The below code will throw compile time error as we have declared the temp local variable as public.

public class Test
{
    public static void main(String[] args) 
    { 
        //Public local variable
        public int temp; // Error Only final is permitted
        System.out.println(temp); 
    }
}

14. Can final method be overloaded in Java?

       Yes, the final method can be overloaded but cannot be overridden. Which means you can have more than one final method with the same name with different parameters.

15. Can we create object for final class?

       Yes, it is possible to create an object for a final class. The best example in Java would be String class. The string is a final class, in almost all code we will be creating the object for it but you cannot extend the String class.

16. What is the main difference between abstract methods and final methods?

Abstract methods are declared in abstract classes and cannot be implemented in the same class. They must be implemented in the subclass. The only way to use an abstract method is by overriding it

Final methods are quite opposite to abstract, final methods cannot be overridden.

17. What is the difference between abstract class and final class?

Abstract Class Final Class
Abstract class can be subclassed and the abstract methods should be overridden Final class cannot be subclassed and the final methods cannot be overridden
Can contain abstract methods Cannot contain abstract methods
Abstract class can be inherited Final class cannot be inherited
Abstract class cannot be instantiated Final class can be instantiated
Immutable object cannot be created Immutable objects can be created
Not all methods of the abstract class need to have a method body (abstract methods) All methods of the final class should have a method body

18. Can we make an abstract method final in Java?

        No, We cannot make an abstract method final in Java because both abstract and final are both extremes as an abstract method must be overridden while the final method cannot be overridden.

19. What is the difference between static and final in Java?

Static Keyword Final Keyword
Static keyword can be applied to a nested class, block, method and variables Final keyword can be applied to class, block, method and variables
We can declare static methods with the same signature in subclass but it is not considered as overriding as there won’t be any runtime polymorphism.

If a subclass contains the same signature as a static method in the base class, then the method of the subclass hides the base class method it is called Method Hiding.

Final class methods cannot be overridden.
Static variable can be changed after initialization Final variable cannot be changed after initialization
Static method or variable can be accessed directly by the class name and doesn’t need any object as they belong to the class Object can be created to call the final method or final variables

20. What is a Static Final variable in Java?

When have declared a variable as static final then the variable becomes a CONSTANT.  Only one copy of variable exists which cannot be changed by any instance.

 

Filed Under: Java Tagged With: Final Keyword, Interview Questions, Java Interview Questions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·