aspose file tools*
The moose likes Java in General and the fly likes How to get  an  specific object instance! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get  an  specific object instance!" Watch "How to get  an  specific object instance!" New topic
Author

How to get an specific object instance!

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear All,
I want to get an object instance in some other class .
Consider this




We can create many instances of A & one particular instance has the state i as 10.
I want to use that A instance in my class B.
Is it possible?
Note however classes A & B don't have any direct relationship between them.
Only thing they both have same super class Panel.

Regards
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6

For just creating an instance you dont need any relationship , you need relationship(extend another class) only when you need method overriding.

A a1=new A();//should solve your requirement.


Swastik
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
For just creating an instance you dont need any relationship , you need relationship(extend another class) only when you need method overriding.

A a1=new A();//should solve your requirement.



I need to have an instance or reference to an instance in my class.
But where is that instance?
It is there in the heap!
I know its memory id like 12edcf34 bla bla bla.
How to get that instance & use it in my class B


Regards
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

REFLECTION ??


Time is what we want the most, but what we use the worst. -- William Penn
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
How?
Regards
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6

Are you talking about hashcode?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

ramya so you don't want a reference of an object in your class but you want to copy an instance of another class in your class. But I think that's not possible...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6

A aref=//you should say how to fetch that from the heap


I dont think this valid, because in aref you can only store reference to the instance/object, not its hashcode or memory id or whatever it is.

What you need is probably this

A aref=new A();
String id=aref.toString();

provided toString() is not overridden in class A.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear All,
ramya so you don't want a reference of an object in your class but you want to copy an instance of another class in your class.


I do want a reference of an object in my class, but the object is currently existing .( I don't want to create a new instance using
A afef=new A() )
How to get a reference to that existing object?
Can we use reflection?
If yes how?
Regards

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6

A bref=aref; // A aref=new A() has been already done.

now bref refers to the same object.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

hey, you know its existing ?? if existing where did you create it?? and if you have already created it and you know that... you just use 'aref' right ??
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2359
    
  50
ramya narayanan wrote:We can create many instances of A & one particular instance has the state i as 10.
I want to use that A instance in my class B.
The obvious and simplest way is to pass a reference of the particular A instance into the B object.

Note however classes A & B don't have any direct relationship between them.
Only thing they both have same super class Panel.
Both extending the same class is not a useful relationship unless the root class keeps a static list of all instances created (and that's incredibly unlikely).

If B needs to know about a particular A instance then you need to have some form of relationship between them. This can be an indirect relationship eg B calls a method in C which returns the A instance or D calls a method in C which returns the A instance and then passes it to B but there has to be some sort of relationship. You can't think I know there's an A object somewhere in memory and so I'll just search the heap until I find it - this sort of thing just isn't allowed in Java and for very good reasons.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11473
    
  16

If the object exists in the heap, then one of two conditions holds

1) There is no active reference to it. It CANNOT be 'found' on the heap, even if you do know the exact memory address (at least to the best of my knowledge).

2) This IS an active reference to it. If so, you can either use that reference, or make a copy of that reference, and use that to get to the object.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get an specific object instance!