• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

superclasses- initializing it's fields in the subclass

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,


... publically via the class directly, ...


That just sounds like a static (class) member, unless I'm not understanding you.
Michael Morris
 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really need the Subclasses? What about using the typesafe enum pattern:

[ March 01, 2003: Message edited by: Ilja Preuss ]
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[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 ]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic