This discussion is archived
1 2 Previous Next 22 Replies Latest reply: Dec 19, 2007 9:11 AM by 807601 RSS

java constant

807601 Newbie
Currently Being Moderated
hello everybody !
i just wanna ask dat to make a constant in java is it neccesary to declare it both static and final
?or only final is enough ?
  • 1. Re: java constant
    PhHein Guru Moderator
    Currently Being Moderated
    Final is enough, but it is silly to have it non-static if yoiu want to use it in more than one class.
  • 2. Re: java constant
    807601 Newbie
    Currently Being Moderated
    Yes, imagine if PI in class java.lang.Math had not been declared static:
    circumference = Math.PI * diameter; //no longer works
    
    Math m = new Math(); //do you learn the new math in grade school?
    circumference = m.PI * diameter; //must write things like this, now.
  • 3. Re: java constant
    807601 Newbie
    Currently Being Moderated
    The idiom is "public static final" for constants and "private static final" for private constants. The other common idioms are singletons and enums.

    There is no sense in creating instance variables for constant values, I don't know how clever the compiler is with final instance variables, but it probably wastes a bit of memory.
  • 4. Re: java constant
    807601 Newbie
    Currently Being Moderated
    pkwooster wrote:
    There is no sense in creating instance variables for constant values, I don't know how clever the compiler is with final instance variables, but it probably wastes a bit of memory.
    Just to clarify (pkwooster I know you didn't mean this) there is nothing wrong with marking instance variables as final--we just don't typically call them constants.
  • 5. Re: java constant
    807601 Newbie
    Currently Being Moderated
    PhHein wrote:
    Final is enough, but it is silly to have it non-static if yoiu want to use it in more than one class.
    Well it's not enough to guarantee that the value will be the same for all instances of that class:
    public class ConstantHolder {
    
       public final int constant;
       public ConstantHolder() {
         constant = new Random().nextInt();
       }
    
    }
  • 6. Re: java constant
    807601 Newbie
    Currently Being Moderated
    endasil wrote:
    PhHein wrote:
    Final is enough, but it is silly to have it non-static if yoiu want to use it in more than one class.
    Well it's not enough to guarantee that the value will be the same for all instances of that class:
    public class ConstantHolder {
    
    public final int constant;
    public ConstantHolder() {
    constant = new Random().nextInt();
    }
    
    }
    But that isn't a constant, it's a random number that can't be changed. :) If you initialize a constant at declaration, it can't be changed.
  • 7. Re: java constant
    795426 Newbie
    Currently Being Moderated
    I'd just like to interject here that final doesn't have quite the same idea of const-ness that C/C++'s const keyword does.

    The final keyword in front of a class/instance member simply implies that once the member is assigned, it cannot be assigned again. It alone does not imply any amount of immutability (a final immutable class gives you actual const-ness).
    final int x = 5;
    final String s = "qwerty";
    final Point p = new Point(0,0);
    
    x = 3;              // Error, cannot reassign x
    s = "asdf"          // Error, cannot reassign s
    
    p = new Point(1,1); // Error, cannot reassign p
    p.x = 3;            // No error! We're not reassigning p
  • 8. Re: java constant
    807601 Newbie
    Currently Being Moderated
    DeltaGeek wrote:
    I'd just like to interject here that final doesn't have quite the same idea of const-ness that C/C++'s const keyword does.
    A long-forgot neuron in my brain is now twitching in pain:
    A * p1
    //versus
    const A * p2
    //versus
    A  * const p3
    //versus
    const A * const p4
    Make the bad man Bjarne go away, daddy!
  • 9. Re: java constant
    807601 Newbie
    Currently Being Moderated
    hunter9000 wrote:
    But that isn't a constant,
    That's the point...I was saying that final in fact isn't enough to make something a constant.
    If you initialize a constant at declaration, it can't be changed.
    Neither can be changed. But I can still do this:
    public class ConstantHolder {
       public final int constant = new Random().nextInt();
    }
    Which is being done at the declaration. So if your point is if you do it at declaration and use a literal on the right side, well then, yes it will be a constant. But static + final guarantees a constant value, whereas with just final you have to go digging to see whether it really is.
  • 10. Re: java constant
    807601 Newbie
    Currently Being Moderated
    One more thing to remember is that a static final primitive can be embedded in other class files. This means that if you change the constant and recompile that file, other files which referred to that constant may not see the new value until they are recompiled.
  • 11. Re: java constant
    807601 Newbie
    Currently Being Moderated
    endasil wrote:
    hunter9000 wrote:
    But that isn't a constant,
    That's the point...I was saying that final in fact isn't enough to make something a constant.
    If you initialize a constant at declaration, it can't be changed.
    Neither can be changed.
    My point was that if you initialize at declaration, the value isn't dependent on the implementation of the class, as it's value can never change. If you set the value in the constructor, it does change, from uninitialized to whatever you initialize it to. That's the difference.
    But I can still do this:
    public class ConstantHolder {
    public final int constant = new Random().nextInt();
    }
    Which is being done at the declaration. So if your point is if you do it at declaration and use a literal on the right side, well then, yes it will be a constant. But static + final guarantees a constant value, whereas with just final you have to go digging to see whether it really is.
    Your use of random values is muddying the waters. The point isn't that you can assign a random value to a non-static final variable, it's that if you assign a final variable at declaration, it will be just as "constant" as a static final variable initialized at declaration. I'm not saying that constants shouldn't be static, there's no reason not to be, except for the compilation pitfall paul pointed out. Just that
    public final double PI = 3.14;
    and
    public static final double PI = 3.14;
    are both just as constant. By making it static, the compiler will force you to initialize it at declaration, but that's a bit like using the compiler as a crutch to me.
  • 12. Re: java constant
    807601 Newbie
    Currently Being Moderated
    hunter9000 wrote:
    My point was that if you initialize at declaration, the value isn't dependent on the implementation of the class
    If by implementation you meant "instance details" then I agree.
    Your use of random values is muddying the waters. The point isn't that you can assign a random value to a non-static final variable, it's that if you assign a final variable at declaration, it will be just as "constant" as a static final variable initialized at declaration.
    Actually as the original poster of that line of thought, I get to decide what my point was. And as reply #5 clearly says, my point in that snippet was to show that having a final non-static instance field doesn't guarantee that that field will be the same for all instances.
    public final double PI = 3.14;
    and
    public static final double PI = 3.14;
    are both just as constant.
    Yes, however in the first case you have to look at the source or the compiled class file to see that PI is globally constant. In the second case just looking at a generated javadoc would tell you that it's globally constant. That's a pretty big difference from the point of view of clarity.
  • 13. Re: java constant
    807601 Newbie
    Currently Being Moderated
    hunter9000 wrote:
    By making it static, the compiler will force you to initialize it at declaration, but that's a bit like using the compiler as a crutch to me.
    So you're telling me this won't compile?
    public class Constants {
       public static final Integer realConstant;
       static
       {
          realConstant = new Integer(4);
       }
    
    }
  • 14. Re: java constant
    807601 Newbie
    Currently Being Moderated
    endasil wrote:
    hunter9000 wrote:
    My point was that if you initialize at declaration, the value isn't dependent on the implementation of the class
    If by implementation you meant "instance details" then I agree.
    Your use of random values is muddying the waters. The point isn't that you can assign a random value to a non-static final variable, it's that if you assign a final variable at declaration, it will be just as "constant" as a static final variable initialized at declaration.
    Actually as the original poster of that line of thought, I get to decide what my point was. And as reply #5 clearly says, my point in that snippet was to show that having a final non-static instance field doesn't guarantee that that field will be the same for all instances.
    I should have said "My point". I'm not trying to put words in your mouth, and I'm not trying to start a fight with you, this is just a technical debate. I agree that if you initialize a constant primitive after declaration, then it can vary between instances. My point was that if you initialize at declaration, there's no difference between static and non-static, other than the compiler giving an error with statics.
    public final double PI = 3.14;
    and
    public static final double PI = 3.14;
    are both just as constant.
    Yes, however in the first case you have to look at the source or the compiled class file to see that PI is globally constant. In the second case just looking at a generated javadoc would tell you that it's globally constant. That's a pretty big difference from the point of view of clarity.
    I generated the javadoc for the example I gave, and got this under field summary:
    static double PI
    double PI2 
    and this under field detail:
    PI
    public static final double PI
    
    PI2
    public final double PI2
    Only the detail notes that they're final, and it does it for both. Have I misunderstood what you mean?
1 2 Previous Next