tags:

views:

110

answers:

5

Hi, 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);
                }
A: 

You must create a 'mutator' to modify private member variables.

class example{
    private string myColor;
    public void changeColor(string newColor){
        myColor = newColor;
    }
}
zipcodeman
Setters and Getters would be a much more common way to do this; see Mickel's code example.
Dean J
mutator is another name for a setter. http://en.wikipedia.org/wiki/Mutator_method >The mutator method, sometimes called a "setter"...
zipcodeman
+3  A: 

If I understand your question correctly... You should create public getters and setters:

public void setMyColor(String color) {
    this.myColor = color;
}

public String getMyColor {
    return this.myColor;
}
Mickel
+2  A: 

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: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/Field.html

Itay
You'd need to explicit cast to `String` in the last line, since `Field`s aren't generic and so get() return a result of type `Object`. You'd also need to catch various types of security exception and be aware that depending on the environment this code might not be able to run at all. It *is* what the OP asked for but it's only responsible to let him know these things and steer him towards more appropriate alternatives.
Andrzej Doyle
A: 

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);
Jay R.
Downvote why? Bad idea?
Jay R.
A: 

It depends where you want to do this. Inside the class you simply whatever with it, e.g:

myColor = "blah blah";

From outside, you need to have some public method generally as other posts indicated. In all cases, you have to be careful if your environment in multi-threaded. Class level variables are not thread safe.

fastcodejava