Java Final Variables - final Keyword with Variables

⏱️ 8 min read • Beginner Level • Lesson 18

Lesson 18 of 124 of Java Tutorial

A final variable in Java is a variable whose value cannot be changed after it has been assigned. The final keyword is commonly used to create constants and to prevent accidental value modification.

Before learning final variables, you should understand Java variables, Java data types, and Java naming conventions.


What is a Final Variable in Java?

A variable declared with the final keyword is called a final variable. Once a value is assigned to a final variable, it cannot be reassigned.

Simple Meaning: A final variable is like a locked box. Once you put a value inside it, you cannot replace that value.
  • final variables cannot be reassigned.
  • They are useful for fixed values.
  • They help prevent accidental changes.
  • static final variables are commonly used as constants.

Syntax of Final Variable

The basic syntax of a final variable is:

FinalVariableSyntax.java
Copy Download
final dataType variableName = value;

Example:

FinalSyntaxExample.java
final int MAX_MARKS = 100;
final double PI = 3.14159;
final String APP_NAME = "ProwessApps";

Final Variable Example

In this example, MAX_MARKS stores a fixed value. We can use it in calculations, but we cannot change it later.

FinalVariableExample.java
Copy Try Download
public class FinalVariableExample {
    public static void main(String[] args) {
        final int MAX_MARKS = 100;
        int obtainedMarks = 85;

        System.out.println("Obtained Marks: " + obtainedMarks);
        System.out.println("Maximum Marks: " + MAX_MARKS);
    }
}

Output:

Obtained Marks: 85
Maximum Marks: 100

Can a Final Variable be Changed?

No. Once a final variable gets a value, it cannot be changed. If you try to assign a new value, Java shows a compilation error.

FinalReassignError.java
Copy Download
public class FinalReassignError {
    public static void main(String[] args) {
        final int age = 20;

        // age = 25; // Error: cannot assign a value to final variable age

        System.out.println(age);
    }
}
Important: A final variable can be assigned only once. After assignment, reassignment is not allowed.

Final Local Variable

A final local variable is declared inside a method, constructor, or block. It must be assigned before it is used and cannot be changed after assignment.

FinalLocalVariable.java
Copy Try Download
public class FinalLocalVariable {
    public static void main(String[] args) {
        final String courseName = "Java Programming";
        System.out.println(courseName);
    }
}

Output:

Java Programming

Final Instance Variable

A final instance variable belongs to an object. Once initialized, it cannot be changed for that object. It can be initialized directly or inside a constructor.

FinalInstanceVariable.java
Copy Try Download
public class FinalInstanceVariable {
    final int rollNumber;

    FinalInstanceVariable(int rollNumber) {
        this.rollNumber = rollNumber;
    }

    void showRollNumber() {
        System.out.println("Roll Number: " + rollNumber);
    }

    public static void main(String[] args) {
        FinalInstanceVariable student = new FinalInstanceVariable(101);
        student.showRollNumber();
    }
}

Output:

Roll Number: 101

Blank Final Variable

A blank final variable is a final variable that is declared without assigning a value immediately. It must be initialized before the constructor finishes.

BlankFinalVariable.java
Copy Try Download
public class BlankFinalVariable {
    final String studentName;

    BlankFinalVariable(String name) {
        studentName = name;
    }

    public static void main(String[] args) {
        BlankFinalVariable student = new BlankFinalVariable("Ayan");
        System.out.println(student.studentName);
    }
}

Output:

Ayan
Note: Blank final variables are useful when the value is fixed for each object but different objects can have different fixed values.

static final Variable in Java

A static final variable belongs to the class and cannot be changed. It is commonly used to create constants.

StaticFinalVariable.java
Copy Try Download
public class StaticFinalVariable {
    static final double PI = 3.14159;

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

Output:

PI: 3.14159

Real-World Use of static final Constants

  • Mathematical constants like PI.
  • Configuration values.
  • Application names.
  • Error codes.
  • Database configuration constants.
Naming Convention: Constants are usually written in uppercase letters, such as MAX_VALUE, PI, and DEFAULT_LIMIT.

Final Reference Variable

If a reference variable is declared as final, the reference cannot point to another object. However, the data inside the object may still be changed if the object is mutable.

FinalReferenceVariable.java
Copy Try Download
class Student {
    String name;
}

public class FinalReferenceVariable {
    public static void main(String[] args) {
        final Student student = new Student();
        student.name = "Ayan";

        System.out.println(student.name);

        // student = new Student(); // Error: cannot assign a new object to final reference
    }
}

Output:

Ayan
Important: Final reference means the reference cannot change. It does not always mean the object data cannot change.

Final Variable vs Normal Variable

Normal Variable Final Variable
Value can be changed multiple times. Value can be assigned only once.
Used when data may change. Used when data should remain fixed.
No special keyword is required. Declared using the final keyword.
Example: int age = 20; Example: final int MAX_AGE = 100;

Difference Between final, static and static final

Keyword Meaning
final Value cannot be reassigned.
static Belongs to class.
static final Class-level constant.

Common Mistakes with Final Variables

  • Trying to reassign a final variable after assigning a value.
  • Declaring a blank final instance variable but not initializing it in every constructor.
  • Thinking a final reference makes the object completely immutable.
  • Not using uppercase naming style for constants.
  • Confusing final variables with static variables.
Summary:
  • A final variable can be assigned only once.
  • The final keyword prevents reassignment.
  • Final local variables are declared inside methods or blocks.
  • Final instance variables belong to objects.
  • Blank final variables must be initialized before object construction completes.
  • static final variables are commonly used as constants.
  • A final reference cannot point to another object, but the object data may still be mutable.

Frequently Asked Questions

A final variable in Java is a variable whose value cannot be changed after it is assigned.

A final variable can behave like a constant when its value is fixed. In Java, constants are commonly created using static final.

The final keyword is used to create a final variable in Java.

No, once a final variable is assigned a value, it cannot be reassigned.

A blank final variable is a final variable that is declared without initialization. It must be initialized before the constructor completes.

A static final variable belongs to the class and cannot be changed. It is commonly used to create constants.

Static final constants should usually be written in uppercase letters with underscores, such as MAX_VALUE or DEFAULT_LIMIT.

No, a final local variable cannot be reassigned after it gets a value.

Yes, a final instance variable can be initialized inside a constructor.

A final reference variable cannot point to another object after assignment, but the data inside the object may still be changed if the object is mutable.

Next step: Learn Java Input and Output

🚀 Continue to Java Input and Output →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Final-variables | Language: Java

Q1. What is commonly used to create constants in Java?
Q2. What will happen in this code?
class Student {
    String name;
}

public class Test {
    public static void main(String[] args) {
        final Student s = new Student();
        s.name = "Ayan";
        System.out.println(s.name);
    }
}
Q3. What is a blank final variable?
Q4. Where can a blank final instance variable be initialized?
Q5. Why are final variables useful?
Q6. Which statement is true about final variables?
Q7. What will be the output of this code?
public class Test {
    static final double PI = 3.14;

    public static void main(String[] args) {
        System.out.println(PI);
    }
}
Q8. Which of the following is a good constant naming style in Java?
Q9. What does final mean for a reference variable?
Q10. Which of the following is the correct syntax for declaring a final variable?
Q11. What will happen if we try to reassign a final variable?
Q12. What will be the output of this code?
public class Test {
    public static void main(String[] args) {
        final int marks = 90;
        System.out.println(marks);
    }
}
Q13. Which statement is true about a final local variable?
Q14. What is a final variable in Java?
Q15. Which keyword is used to declare a final variable in Java?

🎉 Great job! Continue learning Java step by step.