aspose file tools
The moose likes Java in General and the fly likes How to get object reference using String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to get object reference using String" Watch "How to get object reference using String" New topic
Author

How to get object reference using String

Jerry Crothers
Ranch Hand

Joined: Mar 06, 2001
Posts: 34
I would like to know if its possible to get a reference to an object without using an array. For example, lets say this code declares a 3 Integaer3:

Integer a = new Integer(1);
Integer b = new Integer(2);
Integer c = new Integer(3);

Instead of using:

System.out.println(a);
System.out.println(b);
System.out.println(c);

I want to output the same using the Strings "a", "b", "c". Can I get the object references using "a", "b" and "c". ?

Is that possible?
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
System.out.println calls an object's toString() method for display. So you can discover what would be printed by using:

....etc. But many classes override the toString() method, so this is not guaranteed to give you the 'object reference', just what would be printed out by System.out.println.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think you may be asking about a map. The Map interface lets you store an object with a key and retrieve it by key later. A string makes a fine key, but any other object can be a key as well.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 944
Or you could make the variables instance fields (i.e. NOT defined inside a method) and then use java.lang.Class and the classes in java.lang.reflect to get at them. Look at Class.getField(String).

Ryan
[ May 04, 2005: Message edited by: Ryan McGuire ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to get object reference using String
 
Similar Threads
Why can't we use the IS-A test for the enhanced for-loop?
Usage of indexOf method of ArrayList class
Integer is subclass of object
How to get object reference using String
I am stuck