Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do I determine the owner of an object?

Reply
Thread Tools

How do I determine the owner of an object?

 
 
Todd
Guest
Posts: n/a
 
      10-10-2007
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

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      10-10-2007
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.

 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      10-10-2007

"Todd" <> wrote in message
news: ups.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/


 
Reply With Quote
 
John Maline
Guest
Posts: n/a
 
      10-10-2007
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
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      10-10-2007
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.
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      10-10-2007
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-11-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
nuby: determine method passed and determine the receiver that received the method Peņa, Botp Ruby 1 01-24-2004 07:51 PM
Are you an Aironet UC4800-E owner? buzz Cisco 1 01-15-2004 09:00 PM
change dir owner Frank McCown Perl 7 12-08-2003 01:33 PM
Re: !! IMPORTANT !! Problem with owner draw listbox Yair Cohen ASP .Net 0 09-04-2003 05:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57