*
The moose likes Beginning Java and the fly likes Is possible to get variable name in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is possible to get variable name in java?" Watch "Is possible to get variable name in java?" New topic
Author

Is possible to get variable name in java?

Eswar Nec
Ranch Hand

Joined: May 02, 2010
Posts: 105
Hi,
I have two class. I want to get first class variable name. Not value.

First.java


Second.java


Is possible?
Help me.
Thanks in advance.


Wake up! Don't let your smile be snatched away by anybody!
Regards, Eswar
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Via reflection : Class#getFields() will return an array of fields. You can get their name via Field#getName()


[My Blog]
All roads lead to JavaRanch
subhash kumar
Ranch Hand

Joined: Jul 14, 2010
Posts: 63
try reflection.


Subhash Kumar
Attitude is everything
Eswar Nec
Ranch Hand

Joined: May 02, 2010
Posts: 105
Thanks Christophe Verré and subhash kumar for your support.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why do you need the variable name?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is possible to get variable name in java?
 
Similar Threads
Final variables
logic behind not able to assign a static value to a non static variable
Dan's GC Question Doubt
Doubt in data encapsulation !!
can the following code produces Null pointer exception?‏