Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java final Variables
Explaination and Examples



In Java, the keyword final is used to mark a variable as unchangeable once it's been assigned. But there's more to it than just being a constant. Understanding final goes beyond syntax—it's about design intent and program safety.

What is a final variable?

A final variable in Java is a variable whose value cannot be changed once it is assigned. This makes it a constant. You can think of it like a locked box—you can put something in it once, but never change it afterward.

Declaring final variables

You can declare a final variable at any of the following levels:

public class FinalVariableExample {
    final int instanceVariable = 10; // instance-level final
    static final int STATIC_CONSTANT = 100; // class-level final

    void show() {
        final int localVar = 5; // local final
        System.out.println("Local final variable: " + localVar);
    }
}

Assigning final variables

Final variables must be assigned exactly once. This can happen:

public class FinalAssignment {
    final int value;

    FinalAssignment() {
        value = 42; // allowed: assigned in constructor
    }

    void display() {
        System.out.println("Final value: " + value);
    }
}
Final value: 42

What happens if you try to reassign a final variable?

Java will throw a compile-time error if you try to assign a value to a final variable more than once.

public class FinalError {
    public static void main(String[] args) {
        final int number = 10;
        number = 20; // Compilation Error: cannot assign a value to final variable
    }
}

Final reference variables

When you make an object reference final, you cannot reassign the reference to another object. However, you can still modify the internal state of the object if it's mutable.

class Book {
    String title = "Java Basics";
}

public class FinalReference {
    public static void main(String[] args) {
        final Book book = new Book();
        book.title = "Advanced Java"; // Allowed: changing state
        // book = new Book(); // Not allowed: reassigning reference
        System.out.println(book.title);
    }
}
Advanced Java

Using final with static variables (constants)

Often, final is combined with static to define constants.

public class MathConstants {
    public static final double PI = 3.14159;
    public static final int MAX_USERS = 100;
}

You access these constants using the class name:

System.out.println(MathConstants.PI); // 3.14159

Best practices

Final vs Constant

A variable declared final is not necessarily a constant unless it is also static. For instance-level final variables, the value is fixed per object, not for the class.

Conclusion

The final keyword in Java is more than just a restriction—it's a promise. It tells the compiler, and future developers, that the variable is safe from change.

QUIZ

Question 1:What is the key characteristic of a final variable in Java?

Question 2:Final instance variables can be assigned within a constructor.

Question 3:Which of the following are valid ways to assign a final variable in Java?

Question 4:Consider the code snippet:
final int speed = 60;
speed = 80;
What will happen at compile time?

Question 5:A final object reference can still be used to modify the internal state of the object it points to.

Question 6:Which of the following statements are true about final variables?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M