This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Is Reflection My Only Option? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is Reflection My Only Option?" Watch "Is Reflection My Only Option?" New topic

Is Reflection My Only Option?

Dave Schick

Joined: Sep 01, 2006
Posts: 13
I have the following scenario (please bear with me on the setup, here)...

Class named "House" has a public static variable named "address". House is a subclass of Building. Building has no variables named "address".

I want to be able to access House.address from Building. Is Reflection my only option? E.g., Building bldg = new House(); I can't say bldg.address to get the House.address variable. Could use reflection, but is there another way that I'm missing?

Goal is to keep Building generic so I can retrieve the "address" of ANY subclass that extends Building.

Any help is appreciated.

Dave Schick (SCJP 5.0, SCWCD)
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 544
Here is my thought on this
If you need Generic approach then "address" attribute should belong to Class Building.
Because every type of building would certainly have an address.


Henry Wong

Joined: Sep 28, 2004
Posts: 20538

Also, it may be better create a method call getAddress() and have the building implement a generic one (or have the method as abstract) -- then the subclasses can override the method.

And BTW, having a *static* variable for address may not be what you want -- as it is likely that different instances of house have a different addresses.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Originally posted by Dave Schick:
I want to be able to access House.address from Building.
Here's your main problem: why does the Building class know anything about the House class? In general a class shouldn't include any knowledge about details of its subclasses.

Anyway, if the Building class has a reference to a House object it doesn't need to do reflection to get the address variable of that object. This code works anywhere:
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here's one way to handle it

Now this is not a good example. In fact "instanceof" is an example of things we like to avoid. The other suggestions above are better.

*Move address up to Building so all Buildings have addresses

*Give Building a getAddress() method that returns nothing, override in House

*Give Building an instance of House - aggregation instead of inheritance

Scroll down to the OO, UML, etc forum for a group who loves to talk the theory and practice of OO design. These suggestions don't just show up in text books, they work better in real apps, too.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Dave Schick

Joined: Sep 01, 2006
Posts: 13
Just to clarify -- the Building and House isn't a real production example, that was just for illustration purposes.

In my production scenario, I will NOT know what types of subclasses are possible, so I can't use instanceof, etc.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
If, as you said, the address variable is public and static, you don't need an instance at all - you can just access it from anywhere using


I have a hard time imagining any situation where this design would make sense. But that's the simplest answer to the question that was asked.
[ October 31, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Is Reflection My Only Option?
jQuery in Action, 3rd edition