Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

Get a variable value from the calling class

Thread view: 
Mouch - 13 Jan 2008 22:05 GMT
Hi,

I'm looking for a way to find a variable value in the calling class...

Ex :

Car myCar = new Car();
car.setName("Audi");
Wheel w = new Wheel ();

(...)

//In the Wheel class, i would like to know the name of the "calling"
Car.

//This is what I want...
String answer = wheel.getCarNameWhoCreateMyInstance(); //the result
would be audi

I know, a simple way would be to add a method in the Wheel class but I
can't...
If someone have an idea...
Thanks for any help.

M. Mina
rzymek@gmail.com - 13 Jan 2008 22:27 GMT
> Hi,
>
[quoted text clipped - 21 lines]
>
> M. Mina

Hi

First of all this seems realy ugly.
It would be posible to get the calling class in this example:

class Car {
public void foo() {
 setName("Audi");
 Wheel w = new Wheel();
}
}

class Wheel{
public Wheel() {
 //Here it would be posible to retrieve the Car instance (with
name=="Audi")
 //from the call stack
}
}

It this what you need?

And really, you should look for a different solution.

Cheers
rzymek
Lew - 14 Jan 2008 00:05 GMT
Mouch wrote:
>> I'm looking for a way to find a variable value in the calling class...
>>
[quoted text clipped - 16 lines]
>> If someone have an idea...
>> Thanks for any help.

> First of all this seems realy ugly.
> It would be posible to get the calling class in this example:
[quoted text clipped - 15 lines]
>
> It this what you need?

That won't work in general, because the Wheel instance cannot guarantee who
called it.  What if it wasn't an instance of Car but of SpaceShuttle or
TvGameShow that invoked it?

The best way to do this is to put a reference in the Wheel class to the
desired information, either via a String as the OP suggests or via a reference
to the Wheel itself.

More generally one could write a proxy class that associates a Wheel and a Car
together, perhaps even Car itself:

public class Car
{
  private Wheel wheel;
  // accessor and mutator methods for the attribute
...
}

Anything that wants to get at the Wheel would have to go through the Car
instance to get it, picking up which Car it is as a result.

Another way is to hold an associative Map <Wheel, Car> that lets you look up
the Car for each Wheel of interest.  The calling logic must have set up the
Map appropriately, of course.

Signature

Lew

Patricia Shanahan - 13 Jan 2008 23:10 GMT
> Hi,
>
[quoted text clipped - 19 lines]
> If someone have an idea...
> Thanks for any help.

You can do it as follows:

1. Create a Throwable in the Wheel constructor.

2. Use its getStackTrace() to get a reference to the calling Car instance.

3. Use its getName() method, or if really necessary look at its wheel field.

However, this closely couples the Wheel implementation to the Car
implementation. Any change in Car implementation could break Wheel. It
will also make it impossible to unit test Wheel separately from Car.

Perhaps you could describe the problem you are trying to solve with this
implementation idea?

Patricia
Roedy Green - 14 Jan 2008 15:18 GMT
>//This is what I want...
>String answer = wheel.getCarNameWhoCreateMyInstance(); //the result
>would be audi

The legit way is to pass a parameter in the constructor.  You might
extend the class to a debugging version that supports the extra field.

See http://mindprod.com/jgloss/trace.html
for techniques to figure out who called you.
Signature

Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com

Martin Gregorie - 14 Jan 2008 20:53 GMT
> Hi,
>
[quoted text clipped - 14 lines]
> String answer = wheel.getCarNameWhoCreateMyInstance(); //the result
> would be audi

Your code fragment doesn't establish any connection at all between the
Car and Wheel instances, so there's no way that the Wheel can know what
type of Car its associated with, let alone that that its associated with
anything.

Are Wheels components of Car or is Car the owner of a set of separate
Wheels? Maybe a Wheel can be used on a Truck or a MotorCycle too? For
all we know both might be parts or sub-assemblies in an inventory, in
case which both are elements of a Bill of Materials structure.

A class that represents a real-world object also needs to represent the
relationships between it and other real-world objects before it can
answer the sort of question you're asking it.

You need to sort the relationship out, redesign one or both classes to
reflect that, add any additional classes that are needed and then
rewrite the code you've shown us to match the relationship. Show us that
and maybe we'll be able to help.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.