| Author |
superclasses- initializing it's fields in the subclass
|
David Duran
Ranch Hand
Joined: Feb 11, 2002
Posts: 122
|
|
Ok, I'm in super brain fart mode. It must be too many donuts from this morning. The situation: I've got a superclass that defines a set of like integer values that are required by several subclasses, each subclass defining their own values. However, the subclasses shouldn't have to be instantiated. I'd like to have those values referenced simply as SubberClass.CONSTANT. Is this even possible? In other words, does anyone have advice on the best way to design this? It seems like I'm asking something that can't be "done" the way I want it: Declare int constants in a super class, extend and define the values in the subclass and reference those values publically via the class directly, not through a new'd instance. Thanks
|
 |
Michael Morris
Ranch Hand
Joined: Jan 30, 2002
Posts: 3451
|
|
Hi David,
... publically via the class directly, ...
That just sounds like a static (class) member, unless I'm not understanding you. Michael Morris
|
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
|
 |
David Duran
Ranch Hand
Joined: Feb 11, 2002
Posts: 122
|
|
Yeah, hahaha, so I'm guessing I can't do that. Since if I declare the variables as static in the superclass, then all the subclasses will share the same value, ie they can't set their own individually. My solution then would be to use public accessor methods which unfortunately can't be static either. I'm trying to get around having to new the subclass because it's not necessary. There's no manipulation of those variables, just retrieval.
|
 |
David Duran
Ranch Hand
Joined: Feb 11, 2002
Posts: 122
|
|
Well hot dang, I just found out about this strange and crazy idea of the "Singleton Pattern". I'm reading up on it on some javaworld articles. If you know of any other good articles on it, I'd appreciate it. I think the Singleton Pattern is exactly what will help me.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Do you really need the Subclasses? What about using the typesafe enum pattern: [ March 01, 2003: Message edited by: Ilja Preuss ]
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Garrett Smith
Ranch Hand
Joined: Jun 27, 2002
Posts: 401
|
|
[Garrett-Smiths-Computer:java/practice/static] garrett% java SubberClass 10 10 0 The thing to remember is that you're hiding the variable and that hiding is limited to the subclass. If you want SubberClass value of the constant, you'll have to use CONSTANT within SubberClass or use SubberClass.CONSTANT. Here's a program that demonstrates the scope hidden static members. In addition to hiding static variables, you can hide static methods, and even instance methods and variables. See Ch 8 of the JLS. It's a stupid trick and totally impractical. I wouldn't do this in a professional setting, or even in my own development, especially if you're prone to donut-induced confusion! (fixed the forum tags) [ March 01, 2003: Message edited by: Garrett Smith ]
|
comp.lang.javascript FAQ: http://jibbering.com/faq/
|
 |
 |
|
|
subject: superclasses- initializing it's fields in the subclass
|
|
|