Bookmark Topic Watch Topic
  • New Topic

How to get object reference using String  RSS feed

 
Jerry Crothers
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 410
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hmm, I'm sure I posted a reply to this earlier today from work. Anyway, System.out.println simply calls an objects toString() method. So you can discover what it will print out like this:

...and so on. However, many classes override toString() so this is not guaranteed to get their 'reference'; just their string respresentation as displayed by println.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Report post to moderator
Originally posted by Stuart Gray:
Hmm, I'm sure I posted a reply to this earlier today from work.


You did: http://www.coderanch.com/t/327043/java/java/object-reference-String

Closing this one.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!