Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8763973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:54:45+00:00 2026-06-13T15:54:45+00:00

I am having some confusion between static final class variable and final instance variable

  • 0

I am having some confusion between static final class variable and final instance variable.

Here is the sample code:-

class Foof{
final int size=3;
final int whuffie;

    Foof()
    {
        whuffie=42; 
    }

    public static void main(String [] args){
        Foof obj1 = new Foof();
        Foof obj2 = new Foof();

        obj1.size=53;    //compile time error
        System.out.println(obj1.size);
        obj2.whuffie=45; //compile time error
        System.out.println(obj2.whuffie);
    }
}

Error:-

ankit@stream:/home/Data/JAVA/practice/src/test.com/test-20121031_static_demystified$ javac Foof.java 
Foof.java:14: error: cannot assign a value to final variable size
obj1.size=53;    //compile time error
    ^
Foof.java:16: error: cannot assign a value to final variable whuffie
obj2.whuffie=45;
    ^
2 errors

So my question is, what is the point of having final instance variables if they can’t have a different values per different instance. I mean if they have same value for each instance then why don’t we declare them as class variable(static final class variable) as this would serve the same purpose and the we don’t need to create objects in order to access them.


EDIT -1:-

class Foof{
    final int size=3;
    final int whuffie;

    Foof()
    {
        whuffie=42;
        size = 23;  //compile-time error.
    }

    public static void main(String [] args){
        Foof obj1 = new Foof();
        Foof obj2 = new Foof();

        //obj1.size=53;    
        System.out.println(obj1.size);
        //obj2.whuffie=45; 
        System.out.println(obj2.whuffie);
    }
}

Error:-

Foof.java:8: cannot assign a value to final variable size
size = 23;
^
1 error

According to the error, i can make out that first size is assigned a value =3 during object creation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:54:47+00:00Added an answer on June 13, 2026 at 3:54 pm

    So my question is, what is the point of having final instance variables if they can’t have a different values per different instance.

    They can, but those values can’t be changed after creation. You can only assign values to final instance fields within instance variable initializers and constructors, and you must assign values to all instance fields that way.

    Note that in your code here:

    Foof()
    {
        whuffie=42;  //compile time error
    }
    

    … the comment is incorrect. That assignment should be perfectly valid.

    Final fields are useful for implementing immutability – which helps make it easy to reason about an object. For example, String is immutable, so if you validate a string and then keep a copy of the reference, you know that validation will still be correct later on.

    Compare that with java.util.Date, where if you really want to have any faith in validation being useful, you need to create a defensive copy of the Date value and not provide the reference to any other code, in case it changes the underlying instant being represented.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some real confusion about events in c#... if I have this code
I am having some confusion with the new keyword,things work fine when I am
ASP.NET Application I am having some confusion with showing images. IE is fully working
Having some issues getting my head around the differences between UTF-8, UTF-16, ASCII and
I am having some confusion with a mysql query. Any help appreciated. My current
I'm having some confusion if a VOIP App can have multiple tcp sockets monitored
I'm having some trouble understanding the difference between asynchronous and synchronous Javascript, and was
I was having some confusion when the book started talking about alphabetizing elements in
Im having some general confusion with encoding on a little tool I'm writing. First
I am having real confusion with some flash banners I'm creating and making the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.