• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get an specific object instance!

 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
REFLECTION ??
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How?
Regards
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you talking about hashcode?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A bref=aref; // A aref=new A() has been already done.

now bref refers to the same object.
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ??
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic