How do I determine the owner of an object?

T

Todd

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
 
S

Stefan Ram

Todd said:
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.
 
M

Matt Humphrey

| 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/
 
J

John Maline

Todd said:
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
 
M

Mark Space

Todd said:
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.
 
P

Patricia Shanahan

Todd said:
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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top