>I want to have a static constant X defined in a superclass A and given
> various different values in subclasses B, C, and so on.
Right here, there's a conceptual problem. Since X is static, there is
only "one of" it. There isn't one for A, another for B, another for C, and
so on. There's just one. And it can take on exactly one value at any point
in time. And in this case, because it's constant, it has to take the same
value for the entire program run.
> When the X is
> accessed in a method of A by class B (through inheritance), I want it
> to get the value of B.X, not A.X. Is this possible?
You would have to declare a different static constant named X in B.
> My initial naive attempt at just defining X separately in each class as
> a static final did not work; A.X is accessed instead.
If you had typed in A.X, then A.X will get access. If you had instead
typed B.X, B.X would get accessed.
> Unless someone
> has a better idea, I'll resort to replacing the constant by a static
> method returning the desired constant instead, and rely on
> polymorphism.
You might have better luck using a non-static method.
- Oliver
bfeist - 23 Oct 2006 21:54 GMT
> >I want to have a static constant X defined in a superclass A and given
> > various different values in subclasses B, C, and so on.
>
> Right here, there's a conceptual problem. Since X is static, there is
> only "one of" it. There isn't one for A, another for B, another for C, and
> so on.
> You would have to declare a different static constant named X in B.
That's what I meant by a shadow variable -- the same name, but declared
and given values in two different classes.
> If you had typed in A.X, then A.X will get access. If you had instead
> typed B.X, B.X would get accessed.
Alas! That's exactly what happened.
> > I'll resort to replacing the constant by a static
> > method returning the desired constant instead
>
> You might have better luck using a non-static method.
What will happen if I use static? Something analagous to my
experiences with static constants, where there's no dynamic binding
based on the run-time class involved?
Thanks,
Bruce Feist
Oliver Wong - 24 Oct 2006 15:04 GMT
>> > I'll resort to replacing the constant by a static
>> > method returning the desired constant instead
[quoted text clipped - 4 lines]
> experiences with static constants, where there's no dynamic binding
> based on the run-time class involved?
Right. The rule of thumb is that things which are static don't get
inherited, and so any rules about inheritance (and thus dynamic binding)
doesn't apply.
- Oliver
>I want to have a static constant X defined in a superclass A and given
> various different values in subclasses B, C, and so on. When the X is
[quoted text clipped - 8 lines]
>
> Any ideas? Thanks in advance.
There is a way to do this, but it's so hideous that I won't explain it, lest
someone actually do it.
If you want polymorphic behavior, use an instance method. If you need the
value to be a constant (because it's used in, say, a case label), you can
have your instance method return the value of the constant,e .g.
class A {
public static void int THE_ANSWER = 42;
public int getMagicNumber {
return THE_ANSWER ;
}
}
class B extends A {
public static void int THE_QUESTION = 41;
public int getMagicNumber {
return THE_QUESTION ;
}
}
Oliver Wong - 23 Oct 2006 21:28 GMT
> class A {
> public static void int THE_ANSWER = 42;
[quoted text clipped - 5 lines]
>
> class B extends A {
The question should be:
public static void int THE_QUESTION = 6 * 9;
> public int getMagicNumber {
> return THE_QUESTION ;
> }
> }
And both the question and the answer should be declared final.
- Oliver
Mike Schilling - 23 Oct 2006 22:25 GMT
>> class A {
>> public static void int THE_ANSWER = 42;
[quoted text clipped - 9 lines]
>
> public static void int THE_QUESTION = 6 * 9;
Better still:
public static void int THE_QUESTION = 42; /* 6 * 9 */
>> public int getMagicNumber {
>> return THE_QUESTION ;
>> }
>> }
>
> And both the question and the answer should be declared final.
(Regis Philbin voice) Is that your final answer?
bfeist - 23 Oct 2006 21:58 GMT
> >I want to have a static constant X defined in a superclass A and given
> > various different values in subclasses B, C, and so on. When the X is
> > accessed in a method of A by class B (through inheritance), I want it
> > to get the value of B.X, not A.X.
> There is a way to do this, but it's so hideous that I won't explain it, lest
> someone actually do it.
Sir, please don't arouse my curiousity like that unless you plan to
satisfy it! I don't care how hideous it is, I must know!
Thanks,
Bruce
Mike Schilling - 23 Oct 2006 22:25 GMT
>> >I want to have a static constant X defined in a superclass A and given
>> > various different values in subclasses B, C, and so on. When the X is
[quoted text clipped - 7 lines]
> Sir, please don't arouse my curiousity like that unless you plan to
> satisfy it! I don't care how hideous it is, I must know!
Use reflection.
Oliver Wong - 24 Oct 2006 15:08 GMT
>>> >I want to have a static constant X defined in a superclass A and given
>>> > various different values in subclasses B, C, and so on. When the X is
[quoted text clipped - 9 lines]
>
> Use reflection.
Beware, though. There is a theory which states that if ever anyone
discovers how to emulate the inheritance of static members via reflection,
the Java language as we know it will instantly disappear and be replaced by
something more bizarrely inexplicable.
There is another theory which states this has already happened
(somewhere between 1.4 and 1.5).
- Oliver