Get a variable value from the calling class

Discussion in 'Java' started by Mouch, Jan 13, 2008.

  1. Mouch

    Mouch Guest

    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
    Mouch, Jan 13, 2008
    #1
    1. Advertising

  2. Mouch

    Guest

    On 13 Sty, 23:05, Mouch <> wrote:
    > 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
    , Jan 13, 2008
    #2
    1. Advertising

  3. Mouch wrote:
    > 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
    Patricia Shanahan, Jan 13, 2008
    #3
  4. Mouch

    Lew Guest

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


    wrote:
    > 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.

    --
    Lew
    Lew, Jan 14, 2008
    #4
  5. Mouch

    Roedy Green Guest

    On Sun, 13 Jan 2008 14:05:54 -0800 (PST), Mouch <>
    wrote, quoted or indirectly quoted someone who said :

    >//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.
    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
    Roedy Green, Jan 14, 2008
    #5
  6. Mouch wrote:
    > 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.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Jan 14, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,711
    Thomas Weidenfeller
    Oct 12, 2005
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,866
    Eric Sosman
    Jan 7, 2005
  3. Navaneeth
    Replies:
    4
    Views:
    543
    Kenny McCormack
    Nov 20, 2010
  4. Pete Mahoney

    How to get Variable value not Variable name.

    Pete Mahoney, Jul 24, 2003, in forum: Javascript
    Replies:
    11
    Views:
    253
    Richard Cornford
    Jul 25, 2003
  5. tnhoe
    Replies:
    4
    Views:
    682
Loading...

Share This Page