0

I have the following situation (I know it doesn't sound real but I simplified it for better understanding):

  • A class A, with a public static int f; declared
  • Other classes B, C, D, ... that extend A
  • A method foo (somewhere else, doesn't matter) with the following signature: int foo(Class< A> classz);

Now I want this method implementation to return the value if the static field f, from the class represented by classz; subclasses of A may have different values. I don't want to use reflection (and neither the new jdk7 invoke.* features). For now, I have this solution that works, but isn't good, since it instanciates an object (which should not be necessary) and it generate a warning by accessing a static field from an instance:

int foo(Class< A> classz) throws [don't matter] {
    return classz.newInstance().f;
}

Any suggestions? Thanks in advance?

PS: Don't know how to type Class< ?> without the space...

1 Answer 1

2

It's not really clear what you're trying to do or why you're passing in a class at all - it looks like you should just need:

int foo() {
    return A.f;
}

On success, that's what your current code will be returning anyway. Your code is equivalent to:

int foo(Class<A> classz) throws ... {
    A ignored = classz.newInstance();
    return A.f;
}

It sounds like you're trying to use polymorphism with static fields - that's simply not going to work. Static members aren't polymorphic, and neither are fields. If you're declaring extra f fields within each subclass, you would have to use reflection to get at them, but I advise you to redesign anyway. If you're not declaring extra f fields within each subclass, then you've only got one field anyway - B.f and C.f will basically resolve to A.f anyway...

Sign up to request clarification or add additional context in comments.

3 Comments

The thing is classes B, C, D can have different values on f. The solution I posted works in the sense that it has this kind of 'polymorphism with static fields'; I mean when you access a static field from an instance it has this 'polymorphism'... (Well, mayble ill try to javap my solution to see how its done in bytecode level)
@FakeFaker: No, they can't have different values for f, unless they're declaring their own variables - in which case you're basically shadowing. It's not a good design and you will need to use reflection. And no, it doesn't have this sort of polymorphism. For example: A a = new B(); int x = a.f; will always use A.f, not B.f. It would be useful if you could clarify whether you really have declared f in the subclasses - because if you haven't, you've only got a single variable and I suspect you've fooled yourself into thinking you've got one per subclass.
Hm...No I didn't declared it in subclasses and I have indeed fooled myself into thinking I have one per subclass :) that was stupid, I'll try to rethink my whole problem.. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.