• Post Reply Bookmark Topic Watch Topic
  • New Topic

Set a string equal to the name of an object or other type?  RSS feed

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to set a string equal to the text name of an object or other java type? For example, if I have an Icon class object named blackRook, is there a way to set a string equal to the string "blackRook" without saying

String a = "blackRook"

?

Is something like this possible:

String a = blackRook.parseString();
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. But that's because Java objects don't have names.

Perhaps you thought that the name of a variable which contains a reference to an object was the name of that object. That isn't the case. For one thing it's possible (and completely normal) for several variables to refer to a single object.

Anyway, why would you want to do that? Whatever you think you need it for, there's probably a reasonable way to solve that problem without trying to get the names of variables into your code. So would you like to ask the question which made you ask this question?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, objects don't have names. Not unless the class contains a field String name; which can be accessed either directly ( ) or through an accessor getName() ( ).

What is your real requirement? (what you need to do, not how you expect/hope to do it.)
 
Myke Enriq
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you want to print the name of the class of a certain object ?

Object o = new Object();
o.getClass().getName(); <- is the name you are looking for
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!