Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Shadow Variables and Inheritance

Reply
Thread Tools

Shadow Variables and Inheritance

 
 
bfeist@speakeasy.net
Guest
Posts: n/a
 
      10-23-2006
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. Is this possible?

My initial naive attempt at just defining X separately in each class as
a static final did not work; A.X is accessed instead. 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.

Any ideas? Thanks in advance.

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      10-23-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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


 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      10-23-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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. Is this possible?
>
> My initial naive attempt at just defining X separately in each class as
> a static final did not work; A.X is accessed instead. 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.
>
> 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 ;
}
}



 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      10-23-2006

"Mike Schilling" <(E-Mail Removed)> wrote in message
news:gj9%g.23143$(E-Mail Removed). net...
>
> class A {
> public static void int THE_ANSWER = 42;
>
> public int getMagicNumber {
> return THE_ANSWER ;
> }
> }
>
> 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


 
Reply With Quote
 
bfeist
Guest
Posts: n/a
 
      10-23-2006
Oliver Wong wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...


> >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

 
Reply With Quote
 
bfeist
Guest
Posts: n/a
 
      10-23-2006
Mike Schilling wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> >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

 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      10-23-2006

"Oliver Wong" <(E-Mail Removed)> wrote in message
news:TB9%g.35865$P7.19624@edtnps89...
>
> "Mike Schilling" <(E-Mail Removed)> wrote in message
> news:gj9%g.23143$(E-Mail Removed). net...
>>
>> class A {
>> public static void int THE_ANSWER = 42;
>>
>> public int getMagicNumber {
>> return THE_ANSWER ;
>> }
>> }
>>
>> class B extends A {

>
> The question should be:
>
> 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?


 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      10-23-2006

"bfeist" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Mike Schilling wrote:
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>> >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!


Use reflection.


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      10-24-2006

"bfeist" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Oliver Wong wrote:
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...

>
>> > 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?


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


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      10-24-2006

"Mike Schilling" <(E-Mail Removed)> wrote in message
news:ara%g.20140$(E-Mail Removed) om...
>
> "bfeist" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>> Mike Schilling wrote:
>>> <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed) oups.com...
>>> >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!

>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Question on VS IDE's "build" and "Shadow Copies and Restart" =?Utf-8?B?RGlmZmlkZW50?= ASP .Net 0 02-03-2006 05:45 PM
Shadow-copied assemblies and dynamic assembly loading Barry Kelly ASP .Net 4 12-07-2004 10:32 AM
"Drop shadow" effect and text on image using JAI Olivier Java 2 07-08-2004 06:34 PM



Advertisments