aspose file tools*
The moose likes Beginning Java and the fly likes Variable names Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable names" Watch "Variable names" New topic
Author

Variable names

Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 289

hi all
I want to get all the variable name in the class
can you tell me? how can i get that in code?


-Chiru
Laxman Guru
Greenhorn

Joined: Feb 26, 2008
Posts: 17
Hi Surya,
use object.getClass().getFields() method.But it returns only the public fields.


Laxman
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 289

thanks
ya i tried this, but i want get all the variables and its type..
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Do you really mean "variables"? Or do you mean "fields"?

In Java, "variable" generally means the local variables used in methods. There is no way in normal Java to find out these names. Depending on how you compiled your class (with or without debug information), there may be no way at all. But very few applications need to know the local variable names. If you think you need to know them, and you are indeed a beginner, it's almost certain that you don't need them, and just need a better design.

In contrast, "field" means the values that are part of an object instance, or a class (static fields). These can be obtained by Java Reflection, using methods of java.lang.Class.

getFields() will get all public fields, inherited or not.

getDeclaredFields() gets all fields, directly declared in the class, whatever their access level.

If you need all fields, in the class and its superclasses, you'll need to code a loop, and use getSuperclass() in the loop.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Laxman Guru
Greenhorn

Joined: Feb 26, 2008
Posts: 17
try object.getDeclaredFields()method...It will return an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields.
For further reference see webpage
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 289

sorry if the question is worng.
i have the JFrame in that i created all the GUI in Netbeans.
i want to make the application like coustmizeing the font and color for panels like that.
so i have more then 50 JLabels so i don't want to hard code it.
i want to write the code to access all the Labels in that Class. and i Want to set the Properties
Owen Luo
Greenhorn

Joined: Mar 19, 2008
Posts: 5
Maybe you can make a new class such as myJLable inherits from the JLabel. and change the type of your 50 JLable into myJLable.

Than you can do something with myJLable to take effect on these 50 lables.
[ March 19, 2008: Message edited by: Owen Luo ]

I am a fresh guy in JAVA with pool english.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Variable names