Skip to content

Final keyword in Java | Variable, Method, Class with examples

  • by

The final keyword in java is used to create a Constant Variable, Prevent method overloading and prevent Class Inheritance, etc. Java Final keyword restrict to the user and it can apply only to: –

  1. Variables
  2. Methods
  3. and Class
Final keyword in Java Variable, Method, Class

Java Final Variable

Defining a variable as a Final makes it constant, which means you can change (modified) the value of it. You must initialize The Final Variables.

Let’s see the Example of the final variable in Java.

There is a variable mark that has used the final keyword. We will try the change the value of it. As per the concept of final the value should not be changed. Once a java final variable value assign then it can’t be changed.

class TestCode {

    //final variable
    final int mark = 90;

    void run() {
        mark = 90;
    }

    public static void main(String args[]) {
        TestCode obj = new TestCode();
        obj.run();

    }
}

Output: Error:(7, 9) java: cannot assign a value to final variable mark

Nowadays IDE is rich with features, where you will get direct messages about any error and problem. See below screenshot showed an error during programming in IntelliJ IDEA.

Java Final Variable example

Java final method

The main advantage of the final method is it can’t override.

Must Read about what is:-

Let’s see the example of how to make the final method and try to override a final method in java.

 class ObjectShapes {
     final void draw(){
         System.out.println("Object Shape");
     }
}

class Rectangle extends ObjectShapes{

    @Override
    void draw() {
        System.out.println("Draw Rectangle");
    }
}

class Shape{
    public static void main(String args[]){
        // object creating
        Rectangle rect1=new Rectangle();
        rect1.draw();
    }
}

Output: Error:(10, 10) java: draw() in Rectangle cannot override draw() in ObjectShapes
overridden method is final

IDE itself shows an error, see below screenshot of code.

Java final method example

Java final class

To prevent your class to be extend use the final keyword with the class. See below example of how to do it. Where ObjectShapes class is a final class and trying to extend, but it will show an error.

 final class ObjectShapes {
      void draw(){
         System.out.println("Object Shape");
     }
}

class Rectangle extends ObjectShapes{

    @Override
    void draw() {
        System.out.println("Draw Rectangle");
    }
}

class Shape{
    public static void main(String args[]){
        // object creating
        Rectangle rect1=new Rectangle();
        rect1.draw();
    }
}

Output: Error:(7, 25) java: cannot inherit from final ObjectShapes

IDE itself will show an error, see the below screenshot.

What is Java’s final parameter (Arguments)?

Answer: A parameter (argument) defined (declared) with the final keyword is called a final parameter. The value of the final parameter can’t be changed.

final class ObjectShapes {
    void draw(int w, final int h) {
        h = h + h; // can't change value
        w = w + w; // can change value
        System.out.println("Object W and H " + w + " " + h);
    }
}


class Shape {
    public static void main(String args[]) {
        // object creating
        ObjectShapes rect1 = new ObjectShapes();
        rect1.draw(2, 3);
    }
}

Output: Error:(3, 9) java: final parameter h may not be assigned

What is Java final parameter?

Does the final constructor exist in Java?

Or Final keyword with constructor?

Answer: Not it’s not possible a Java final constructor, because the constructor is never inherited.

Does final constructor exist in Java

Q: Can we use the final array in java?

Answer: You can declare Java Final Array without any problem. The final array of elements can change. Where Arrays are objects and object variables (elements of the array) are always references in Java. 

Let’s see one simple example.


public class HelloTest {

    public static void main(String[] args) {
        final int arr[] = {1, 2, 3, 4, 5};  // Note: arr is final
        arr[2] = 4; //change the value of index 2

    }
}


Q: What is a blank or uninitialized final variable?

Answer: Which Final variables are not initialized at the time of declaration is called a blank final variable. It can be initialized only in the constructor.

Example of blank final variable:-

class Employee{  
int id;  
String name;  
final String PAN_CARD_NUMBER;  
...  
}  

Let’s initialize the blank final variable.


class TestBank {
    final int intrestRate;//blank final variable

    TestBank(){
        intrestRate=7;
        System.out.println(intrestRate);
    }

    public static void main(String args[]) {
        new TestBank();
    }

}

Q: What is a static blank final variable?

Answer: A static final blank variable is not initialized at the time of declaration. It can be initialized only in static blocks.

Example of static blank final variable:-

class TestBank {
    static final int accounts;//static blank final variable

    static {
        accounts = 10250;
    }

    public static void main(String args[]) {
        System.out.println(TestBank.accounts);
    }

}

Tricky Question: Does the final method inherited?

Answer: Yes, you can inherit the final method but can’t override it. See below example:-

class ObjectShapes {
    void draw() {
        System.out.println("Method ");
    }
}

class Shape extends ObjectShapes {
    public static void main(String args[]) {
        new ObjectShapes().draw();
    }
}

Conclusion

You learned about the final keyword means for classes, methods, and variables in Java. You may not use the final keyword often in Java programming, it can be a good design solution.

Do comment if you have any doubts and suggestions on this tutorial.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java Final keyword examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading