Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, how to get attribute given the string with its name?

Tags:

I'm sorry for asking this sort of questions, but I really couldn't find the answer in Google. So say I have a class with private String myColor and I have a string "myColor". Now I want to manipulate the myColor attribute. How can I do that?

Edit: Sorry for an unclear question, I guess the best way is to explain what I need it for. I've got a Swing form and want to use the preferences api to set the values of fields when loading gui. So I can read all the fields and then do outputDirectoryTextField.setText(valueFromPrefsAPI); for each of them, but that seems to be a bit of unneeded coding so I want to have an array(hash?) with the names of fields and loop through them, like this:

String[] myTextInputs = {"thisInput", "thatInput"};  for (String inputName : myTextInputs) {     String value = prefs.get(inputName, "");     /* some code I'm seeking to find out*/.setText(value); } 
like image 506
Fluffy Avatar asked Jan 11 '10 17:01

Fluffy


People also ask

What is name attribute in Java?

The attribute identifier, also called attribute name, is a string that identifies an attribute. An attribute value is the content of the attribute and its type is not restricted to that of string. You use an attribute name when you want to specify a particular attribute for either retrieval, searches, or modification.

How do you call an object attribute in Java?

Variables that belong to an object are usually called attributes, but you might also see them called “fields”. To access an attribute of an object, Java uses dot notation. For example: int x = blank.

What is Java attribute code?

An attribute is another term for a field. It's typically a public constant or a public variable that can be accessed directly. In this particular case, the array in Java is actually an object and you are accessing the public constant value that represents the length of the array.


2 Answers

You can use reflection to inspect the content of any object, as follows:

Object o = ...; // The object you want to inspect Class<?> c = o.getClass();  Field f = c.getDeclaredField("myColor"); f.setAccessible(true);  String valueOfMyColor = (String) f.get(o); 

Note that getDeclaredField() will only return field's declared by the object's class. If you're looking for a field that was declared by a superclass you should loop over all classes of the object (by repeatedly doing c = c.getSuperclass() until c == null)

If you want to change the value of the field you can use the set method:

f.set(o, "some-new-value-for-field-f-in-o") 

Additional details: https://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Field.html


https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getField(java.lang.String)

You can use getField(...) which will search on super class if not found in class.

like image 156
Itay Maman Avatar answered Sep 28 '22 07:09

Itay Maman


Based on the edit, my suggestion is to use a Map to contain a map of preference name to appropriate text field or other text component. Just build the map when you build the user interface.

Map<String, JTextField> guiFields = new HashMap<String, JTextField>(); 

Then you can have the code do

guiFields.get(inputName).setText(value); 
like image 41
Jay R. Avatar answered Sep 28 '22 09:09

Jay R.