1

Consider the following code:

public class Test {

    static private volatile Integer number1 = 42;
    static private volatile Integer number2 = 42;

    public static void main(String[] args) {
        Test test = new Test();
        test.changeInteger(number1);
        System.out.println(number1);
    }

    public void changeInteger (Integer number) {
        number = new Integer(3);
    }
}

I would like to change the value of either number1 or number2 depending on the argument I pass to the method changeInteger. Obviously this doesn't work. Is there a way to make this work in Java without using reflection?

To clarify: I want the call changeInteger(number1) to change the field number1.

Roland
  • 7,525
  • 13
  • 61
  • 124
  • 3
    You have several choices, use an array, use an if/else statement, use a switch statement.. however, more importantly, why? – Chris K Sep 23 '14 at 12:42
  • 4
    possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Smutje Sep 23 '14 at 12:43
  • @ChrisK My actual code is a bit more complicated. The fields are actual WeakRerences that will be accessed by multiple threads through a double checked locking pattern. So I want an easy way to parameterize which field is going to be updated. – Roland Sep 23 '14 at 12:46
  • 1
    @Roland in that case you may be served best by a simple Map, mapping the 'field name' to the 'field value'. – Chris K Sep 23 '14 at 12:48
  • 1
    @Roland but to answer your question, yes the fields can be selected dynamically without using reflection in Java by using Unsafe. Frankly I would change the design or use reflection at that point ;) – Chris K Sep 23 '14 at 12:51
  • 1
    In Java 8 one can use method references, which is nicer. That would involve storing the field name and the reference in a map. So once again, not great. – Chris K Sep 23 '14 at 12:52
  • @ChrisK What do you mean by Unsafe? – Roland Sep 23 '14 at 12:53
  • 1
    @Roland ua-ooo, can of worms ;) sun.misc.Unsafe, is a back door into a lot of low level goodies in Java.. it is an interesting area to research, but be warned.. mis-use of it can crash the JVM. – Chris K Sep 23 '14 at 12:55
  • @ChrisK wow, first time ever I learn about this. Thanks for the link, but I agree this is probably not a good idea. – Roland Sep 23 '14 at 12:57
  • A simple `if` statement would do. – Raedwald Sep 23 '14 at 22:04

3 Answers3

4
public class Test {
    public Integer[] number = {42, 42};
    public static void main(String[] args) {
        Test test = new Test();             //you create a new instance
        test.number[0] = new Integer(3);    //change first element to 3
        test.number[1] = new Integer(2);    //change second element to 2
        System.out.println(test.number[0]); //print first element
    }
}

About arrays: manual Demo : http://ideone.com/aKzQI0

Margus
  • 19,694
  • 14
  • 55
  • 103
2

You will need to return the value of the variable from the method. Otherwise the change in value of variable is restricted the method call stack of 'changeInteger' method.

 public static void main(String[] args) {
        Test test = new Test();
        number1 = test.changeInteger(number1);
        System.out.println(number1);
    }

    public Integer changeInteger (Integer number) {
        return new Integer(3);
    }
SJha
  • 1,510
  • 1
  • 10
  • 17
2

Just to add to @Margus's answer. In my books, if a variable name ever contains a number, like number1, that's a sign that you aren't doing it right. Use an array.

On the other hand, if your variables are actually named, you may be better of using a Hashtable. The names of fields in a class are meant to be hard-coded. If you ever need non-hard-coded "field names", then they aren't really fields. They are data! So hashtable / dictionary is the correct route in Java.

Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58