Re: How do I determine the owner of an object?
"Todd" <todd.heidenthal@lmco.com> wrote in message
news:1192044674.406368.227530@22g2000hsm.googlegroups.com...
| 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/