Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

A final is final as soon as you asign a value to it. You can't change it later.

You can't modify a final variable after initializing on declaration or in the constructor(s).

Using the static keyword doesn't make it modifiable. It just means the final variable can be accessed through the class name or instance variable, the variable is still immutable.

Actually static and non static final variable are needs when we are creating any varible as a class specific then will declare it as static and if object level then will declare it as non static variable.

For example, We have a country as a class and and will have two data members of that class like timezone and gravity. we declred both of them as final, but timezone is object specific(timezone of every country is different but same throughout the country) and gravity is class specific(gravity of each country is same as earths gravity) so we are declared gravity as static final.

final variables are typically used to define things that should never change. You can write to it one time and then it is set forever.

You might use this in a constructor to set an ID for an object or something similar.

For final variables you can assign value only once. It is generally used in case where you don't want the values of variables to be changed later in your program.

Static variables only one instance is created per class, irrespective of number of objects of that class you create.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top