How do I determine the owner of an object?

Discussion in 'Java' started by Todd, Oct 10, 2007.

  1. Todd

    Todd Guest

    Hello,

    If it can be done, how do I determine the class within which an object
    was instantiated?

    For example:

    class Example1
    {
    public Example1(){}

    Double getValue()
    {
    return value;
    }

    Double value = new Double( 12.34 );
    }

    class Example2
    {
    public Example2(){}

    Double getValue()
    {
    return value;
    }

    Double value = new Double( 12.34 );
    }

    class LookAtMe
    {
    Example1 ex1 = new Example1();
    Example2 ex2 = new Example2();

    Vector<Double> values = new Vector<Double>();
    values.add( ex1.getValue() );
    values.add( ex2.getValue() );

    Iterator<Double> valueIter = values.iterator();
    while( valueIter.hasNext() )
    {
    Double value = valueIter.next();

    /*************************************** HERE IT IS
    ***************************************/
    System.out.println( "value's owner class is: "
    + ??????????????? );
    }
    }


    The desired output would be:
    Example1
    Example2

    Any ideas?
    Todd
     
    Todd, Oct 10, 2007
    #1
    1. Advertising

  2. Todd

    Stefan Ram Guest

    Todd <> writes:
    >If it can be done, how do I determine the class
    >within which an object was instantiated?


    Given only the object, in the general case,
    it can not be done.
     
    Stefan Ram, Oct 10, 2007
    #2
    1. Advertising

  3. "Todd" <> wrote in message
    news:...
    | Hello,
    |
    | If it can be done, how do I determine the class within which an object
    | was instantiated?
    |
    | For example:
    |
    | class Example1
    | {
    | public Example1(){}
    |
    | Double getValue()
    | {
    | return value;
    | }
    |
    | Double value = new Double( 12.34 );
    | }
    |
    | class Example2
    | {
    | public Example2(){}
    |
    | Double getValue()
    | {
    | return value;
    | }
    |
    | Double value = new Double( 12.34 );
    | }
    |
    | class LookAtMe
    | {
    | Example1 ex1 = new Example1();
    | Example2 ex2 = new Example2();
    |
    | Vector<Double> values = new Vector<Double>();
    | values.add( ex1.getValue() );
    | values.add( ex2.getValue() );
    |
    | Iterator<Double> valueIter = values.iterator();
    | while( valueIter.hasNext() )
    | {
    | Double value = valueIter.next();
    |
    | /*************************************** HERE IT IS
    | ***************************************/
    | System.out.println( "value's owner class is: "
    | + ??????????????? );
    | }
    | }
    |
    |
    | The desired output would be:
    | Example1
    | Example2

    Java does not include any notion of the "owner" of an object. Objects do
    not naturally have any reference to the object that was in play during their
    construction, except for the special construction of inner objects which
    have an implicit reference to their outer context. There is also no reverse
    reference information that indicates which objects have references to some
    object, as there are plenty of times when "owner" simply means an object
    that contains the reference and not necessarily the context of construction.

    However, you can represent that information directly yourself by including
    whatever you want to designate as ownership information in the object or
    otherwise storing it someplace.

    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Oct 10, 2007
    #3
  4. Todd

    John Maline Guest

    Todd wrote:
    > If it can be done, how do I determine the class within which an object
    > was instantiated?


    Programmatically? You can't unless the object has an "owner" field that
    you set somehow. It's up to you.

    Debugging? If this is a debugging problem, then a profiler may be able
    to answer that question. For example, if you're trying to track a
    memory leak and understand where the allocation is happening. A tool
    like NetBeans Profiler allows you to track objects and can tell you what
    method allocated them. This uses specialized capabilities that only
    exist when you're running with the profiler enabled. It's not something
    the java runtime tracks normally.

    Good luck,
    John
     
    John Maline, Oct 10, 2007
    #4
  5. Todd

    Mark Space Guest

    Todd wrote:

    > If it can be done, how do I determine the class within which an object
    > was instantiated?
    >
    > The desired output would be:
    > Example1
    > Example2


    None such, as others have said. Don't forget though that there's no
    difference between using a reference of type Double and using a
    reference of type Example1. (I think Array references typically do use
    up a bit more memory though.) So if you have the parent reference
    anyway, you might as well pass that.

    For example:

    Example1 ex1 = new Example1();
    Example2 ex2 = new Example2();

    Vector<Double> values = new Vector<Double>();
    values.add( ex1.getValue() );
    values.add( ex2.getValue() );

    Try this:

    interface Example {
    public Example getParent() {};
    }

    class Example1 extends Double implements Example
    {
    Double value = new Double( 12.34 );
    public String toString() {
    return value.toString();
    }
    Example1 getParent() {
    return this;
    }
    }

    And similar for Example2. Then you can:

    Iterator<Double> valueIter = values.iterator();
    while( valueIter.hasNext() )
    {
    Double value = valueIter.next();

    System.out.println( "value's owner class is: "
    + ((Example)value).getParent()
    .getClass().simpleClassName() );
    }
    }

    I think this technique might work. Haven't tested it out in detail.
     
    Mark Space, Oct 10, 2007
    #5
  6. Todd wrote:
    > Hello,
    >
    > If it can be done, how do I determine the class within which an object
    > was instantiated?

    ....

    As indicated in other messages, Java does not have a concept of object
    ownership.

    However, you presumably want to achieve some objective using the data,
    if you could get it. It is possible that there is some other way of
    achieving that objective that does not depend on a concept of object
    ownership.

    If you would suggestions, please describe the higher level objective.

    Patricia
     
    Patricia Shanahan, Oct 11, 2007
    #6
  7. Todd

    Roedy Green Guest

    On Wed, 10 Oct 2007 12:31:14 -0700, Todd <>
    wrote, quoted or indirectly quoted someone who said :

    >If it can be done, how do I determine the class within which an object
    >was instantiated?


    There is no such concept as "owner" in Java.

    What you can do is use x.getClass() to get the class that was used
    in new to instantiate the object x. You can then System.out.println
    it.

    To find the chain of superclasses see
    http://mindprod.com/jgloss/classforname.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 11, 2007
    #7
    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. Yair Cohen
    Replies:
    0
    Views:
    2,036
    Yair Cohen
    Sep 4, 2003
  2. Frank McCown

    change dir owner

    Frank McCown, Nov 30, 2003, in forum: Perl
    Replies:
    7
    Views:
    2,020
  3. Dexter

    how to do postback in owner page?

    Dexter, Oct 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    379
    Karl Seguin
    Oct 4, 2004
  4. Saber
    Replies:
    1
    Views:
    335
    Juan T. Llibre
    Aug 20, 2005
  5. Peña, Botp
    Replies:
    1
    Views:
    242
    Robert Klemme
    Jan 24, 2004
Loading...

Share This Page