1

Is it possible to access a child class constant from within a static method in the parent class?

public class Model { public static void someMethod(){ HERE I WANT TO GET THE MODEL_NAME constant! } } public class EventModel extends Model { public static final String MODEL_NAME = "events"; } 

and in some other place I call:

EventModel.someMethod(); 
3
  • 3
    Give an example. I'm wary of any parent that needs to know about its child; ask my dad. Commented Jan 7, 2012 at 2:26
  • Having posted my answer below, I must say I also agree with Dave Newton. If your parent class needs to know about it's child, you MAY have a design issue to consider (but not always . . .) Commented Jan 7, 2012 at 3:26
  • Can you present a situation where this is not a design issue? Commented Jan 7, 2012 at 3:36

2 Answers 2

3

Try it!

If the constant is declared private, then no. If it is public, then yes, as anyone can access it. The parent class is largely irrelevent here.

class Parent { public static void Foo() { int x = Child.YEP; // Ok int y = Child.NOPE; // Error } } class Child extends Parent { public static final int YEP = 42; private static final int NOPE = 66; } 

Foo is defined in Parent, and thus cannot access private members of Child.

How about this?

class Parent { abstract String getModelName(); public void someMethod() { String myModel = getModelName(); } } class Child extend Parent { String getModelName() { return "events"; } } 

Note however, that the method is no longer public.

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

3 Comments

A method name must start with lower case letter.
Foo() should be foo().. It is impolite to don't follow the Java naming rules. I tried to edit the post instead of commenting but I couldn't.
Why? It compiles just fine as-is on Eclipse Java Compiler 0.894_R34x, 3.4.2 release
1

You might find this more effective.

Define your parent class with a method getName. Note that this can be public, if you want your model class to expose a Name property, otherwise, you can keep it as "protected" as I have here. "Protected" will keep the method visible within this class, and any derived (child) classes.

public class Model { private static String MODEL_NAME = "Model"; protected String getModelName(){ return MODEL_NAME; } } 

Then define an "override" for the name method on your child class:

public class EventModel extends Model { private static String MODEL_NAME = "events"; @Override // Tells the compiler that this method OVERRIDES the parent method public String getModelName(){ return MODEL_NAME; } } 

This compiles and runs the way I suspect you are trying to acheive . . .

EDIT: Oooh. NOW I see the problem. Missed that you needed to reference that from a static method . . .

1 Comment

Bad answer above. I missed the fact that you needed to access this from a static method on the parent. My bad . . .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.