Get a variable value from the calling class

M

Mouch

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
 
R

rzymek

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



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
 
P

Patricia Shanahan

Mouch said:
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.

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
 
L

Lew

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?

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.
 
M

Martin Gregorie

Mouch said:
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
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,131
Latest member
IsiahLiebe
Top