Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

We call static final fields as constant, rather I should say Class constant. So can we call just final fields as Object's constant? Is it right to say this?

Edited ::

Consider the following code :

class JustFinal
{
    final int a=20;
    final int b;

    JustFinal(int x, int y){
        //a=x;
        b=y;
    }

    public static void main(String arg[])
    {
        JustFinal jf = new JustFinal(100,200);
    }
}

In the above code field a will have a same value for all the object instances and field b can have a different value for a different object instance. Shouldn't we call the final field a as CONSTANT and final field b as OBJECT's CONSTANT?

share|improve this question
1  
I think you're talking about final fields (or members), not variables; "Variables" usually refers to variables inside a method (Which can also be final). –  aviv Aug 8 '13 at 7:07
    
@aviv : Thanks for the correction. I have made an edit for the same in my question. –  mav3n Aug 8 '13 at 10:54
add comment

1 Answer

up vote 2 down vote accepted

Any value that is determined at compile time and is unable to be changed can be considered a constant.

In your example code, you shouldn't be using final by itself at the class level if you are trying to have all instances of the object have the same value for a. Using the static modifier means that the value is only initialised once and then shared among objects of the same type, you should also make it private unless you want to share the constant with other classes. In your example, each instance of JustFinal will initialise int a=20; rather than all using the same copy (although the JIT will probably optimise this out for you).

Ultimately you can call a and b whatever you like, as long as people understand you. But if you used the phrase "objects constant" in conversation with me I'd have no idea what you were talking about.

The difference between a constant (defined at compile time) and a variable that, once assigned cannot have it's value changed, is mutability (not to be confused with immutable objects).

I'd use the term "immutable property/variable" over "objects constant".

share|improve this answer
    
We know that we can assign a value to a final variable only at 2 places : i) Either we assign it a value at the time of Declaration; OR ii) We assign it a value in a Constructor. If we choose the Option (i) we can't assign it different value for different object. And if choose option (ii) we can have a different value of a final variable for different object. Doesn't the Option (i) makes the final variable a CONSTANT as it have the same value for all the instances of that class? –  mav3n Aug 8 '13 at 6:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.