A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
Is possible to get variable name in java?
Eswar Nec
Ranch Hand
Joined: May 02, 2010
Posts: 105
posted
Jul 29, 2010 23:58:19
0
Hi,
I have two class. I want to get first class variable name. Not value.
First.java
Class First { public String publicName; private String privateName; }
Second.java
Class Second { public static void main(String args[]) { //Here i need fist class varaible name(myName). Not value. } }
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: 14669
11
I like...
posted
Jul 30, 2010 00:01:49
0
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
posted
Jul 30, 2010 00:03:31
0
try reflection.
Subhash Kumar
Attitude is everything
Eswar Nec
Ranch Hand
Joined: May 02, 2010
Posts: 105
posted
Jul 30, 2010 00:32:54
0
Thanks
Christophe Verré and subhash kumar for your support.
public class Second { public static void main(String args[]) { Field fld[] = First.class.getDeclaredFields(); for (int i = 0; i < fld.length; i++) { System.out.println("Variable Name is : " + fld[i].getName()); } } }
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
I like...
posted
Jul 30, 2010 04:50:29
0
Why do you need the variable name?
I agree. Here's the link:
http://aspose.com/file-tools
subject: Is possible to get variable name in java?
Similar Threads
Dan's GC Question Doubt
can the following code produces Null pointer exception?
Final variables
logic behind not able to assign a static value to a non static variable
Doubt in data encapsulation !!
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter