Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Instance Variable as Final in Java
Next article icon

Instance Variable as Final in Java

Last Updated : 25 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

As we all know, when the value of a variable varies from object to object, then that type of variable is known as an instance variable. The instance variable is declared inside a class but not within any method, constructor, or block.

If we don't initialize an instance variable, then the JVM automatically provides a default value according to the data type of that instance variable. But if we declare an instance variable as final, then we must take care of its initialization behavior.

Why Use Final Instance Variables?

Using final ensures that the variable’s value cannot be changed once it is assigned. There is no strict requirement to make the variable final. When it is clear that you explicitly intend never to change the variable, it is considered good practice to make it final. Sometimes we use final instance variables in the creation of immutable classes.

Important Points About Final Instance Variables

1. Initialization is Mandatory

If the instance variable is declared as final, then we must perform initialization explicitly. The JVM does not provide any default value for final instance variables.

Example: Missing Initialization

Java
// Java program to illustrate the behavior // of final instance variable class Test {     final int x;  // Not initialized      public static void main(String[] args) {         Test t = new Test();         System.out.println(t.x); // Compilation error     } } 

Output:

error: variable x not initialized in the default constructor


2. Initialization at Declaration

We can initialize a final instance variable at the time of declaration.

Example:

Java
// Java program to illustrate that   // final instance variable can be initialized at declaration class Test {     final int x = 10;      public static void main(String[] args) {         Test t = new Test();         System.out.println(t.x);     } } 

Output
10 


3. Initialization Inside an Instance Block

We can also initialize a final instance variable inside a non-static or instance block.

Example:

Java
// Java program to illustrate that   // final instance variable can be  // initialized in an instance block class Test {     final int x;      {         x = 10;     }      public static void main(String[] args) {         Test t = new Test();         System.out.println(t.x);     } } 

Output
10 


4. Initialization Inside the Constructor

We can initialize a final instance variable inside a constructor.

Example:

Java
// Java program to illustrate that   // final instance variable can be initialized in constructor class Test {     final int x;      Test() {         x = 10;     }      public static void main(String[] args) {         Test t = new Test();         System.out.println(t.x);     } } 

Output
10 


Invalid Ways to Initialize Final Instance Variables

1. Cannot Be Initialized in a Static Block

Since instance variables belong to objects and static blocks run before object creation, it is not possible to initialize a final instance variable in a static block.

Example:

Java
// Java program to illustrate that we can't initialize   // final instance variable in a static block class Test {     final int x;      public static void main(String[] args) {                  // Cannot assign to non-static          // final field from static context         x = 10;           Test t = new Test();         System.out.println(t.x);     } } 

Output:

error: non-static variable x cannot be referenced from a static context


2. Cannot Initialize in Method

Final instance variables cannot be assigned inside instance methods after object construction, because they must be assigned before the constructor finishes.

Example:

Java
// Java program to illustrate that we can't initialize   // final instance variable in a method class Test {     final int x;      public void m() {         x = 10;      } } 

Output:

error: cannot assign a value to final variable x

Next Article
Instance Variable as Final in Java

B

Bishal Dubey
Improve
Article Tags :
  • Misc
  • Java
  • Java-final keyword
Practice Tags :
  • Java
  • Misc

Similar Reads

    Instance Variable Hiding in Java
    One should have a strong understanding of this keyword in inheritance in Java to be familiar with the concept. Instance variable hiding refers to a state when instance variables of the same name are present in superclass and subclass. Now if we try to access using subclass object then instance varia
    2 min read
    final variables in Java
    In Java, we can use final keyword with variables, methods, and classes. When the final keyword is used with a variable of primitive data types such as int, float, etc), the value of the variable cannot be changed.  Example 1: Usage of final with primitive datatype Java // Java Program to illustrate
    2 min read
    Final Static Variable in Java
    When the value of a variable is not varied, then it is not a good choice to go for an instance variable. At that time, we can add a static modifier to that variable. Whenever we declare a variable as static, then at the class level, a single variable is created which is shared with the objects. Any
    3 min read
    Final Local Variables in Java
    In Java, a local variable is a variable, which is declared inside a method. Local variables are only accessible within the method in which they are declared, other methods in the class do not know anything about that variable. When we declare a local variable, we need to initialize it first before u
    3 min read
    Assigning values to static final variables in Java
    Assigning values to static final variables in Java: In Java, non-static final variables can be assigned a value either in constructor or with the declaration. But, static final variables cannot be assigned value in constructor; they must be assigned a value with their declaration.For example, follow
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences