• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable names

 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
I want to get all the variable name in the class
can you tell me? how can i get that in code?
 
Laxman Guru
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Surya,
use object.getClass().getFields() method.But it returns only the public fields.
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
ya i tried this, but i want get all the variables and its type..
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Laxman Guru
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic