0

I'm writing a method in Java that I want to simulate references.. Since java doesn't have the out keyword like in C# and doesn't have References/Pointers like in C++, I want to know if I can use reflection to simulate it.

An example would be:

public static boolean ChangeValue(Object Input, Object Output) {
    //Use some reflection here to change the value of Output?
    return true;
}

then in main:

public static void main(String[] args) {
    int I = 0;
    ChangeValue(I, I);
    System.out.println(I);
}

So.. Can I use reflection to change the value of any parameter passed to my ChangeValue method? I do not want to return Composite objects..

Brandon
  • 22,723
  • 11
  • 93
  • 186

3 Answers3

3

Java is pass-by-reference-value, meaning that if you pass an Object to a method and modify it, the object "outside" of the method will be modified as well (because the Object is simply a reference). An example of this is calling Arrays.sort on an array.

However, this is not the case for primitives (like int), in which case Java is pass-by-value, so your example with int I wouldn't work. In order to modify the value of int I, you should return an int from your method, and set it to I accordingly.

This means that reflection is not necessary in order to modify the object's value from within the ChangeValue method; anything done to the object within the method, will in effect, modify the object in the scope of the main method.

FThompson
  • 28,352
  • 13
  • 60
  • 93
  • :S I tried passing Integer and changing the value. It does not affect anything in main. – Brandon Jan 31 '13 at 03:35
  • @CantChooseUsernames Correct, and nothing in this answer implies that it would. – Dave Newton Jan 31 '13 at 03:36
  • @CantChooseUsernames In the second paragraph of my answer, I explain how primitives (like `int`) are not pass-by-reference-value. You should instead return a value and set it to `I` (as my answer explains). – FThompson Jan 31 '13 at 03:36
  • "anything done to the object within the method, will in effect, modify the object in the scope of the main method." I think read this wrong. Integer is a class and int is a primitive type. – Brandon Jan 31 '13 at 03:37
  • "Object" is the key word there; otherwise, you read it correctly. Primitives (`int`, `long`, `double`, `char`, `boolean`, etc) **are not** objects, and are passed by value rather than by reference value. – FThompson Jan 31 '13 at 03:38
  • Yes but things such as String and Integer and Double are not primitive :S I thought Box types are not primitive? – Brandon Jan 31 '13 at 03:40
  • Strings are immutable, and while box types are technically objects, they are still passed by value. – FThompson Jan 31 '13 at 03:43
1

No, you cannot.

You can change properties of objects you pass to a method, but you cannot change what the calling code's parameter references–just properties of what it references.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    @CantChooseUsernames And I'm saying you can't; you cannot change what the calling code references, only properties of what it references, from the called method. It's the same thing with an int; you cannot change the value of a variable in the calling code. – Dave Newton Jan 31 '13 at 03:34
1
    public static boolean changeValue(ArrayList<String> input, ArrayList<String> output) {
        //this changes a property on output, but does not change the reference to output
        output.addAll(input);


        return true;
    }


    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<String>();
        ArrayList<String> list2 = new ArrayList<String>();

        list1.add("Foo");

        System.out.println("List 2: " + list2);
        System.out.println("List 2 size:" + list2.size());

        changeValue(list1, list2);

        System.out.println("List 2: " + list2);    
        System.out.println("List 2 size:" + list2.size());
    }

Would print...

List 2: ArrayList@0x23409

List 2 size: 0

List 2: ArrayList@0x23409

List 2 size: 1

rtcarlson
  • 424
  • 4
  • 13
  • +1 I like this. I will just give up and make a class with type and modify it :( Was really hoping there was a way to just do it without work arounds.. – Brandon Jan 31 '13 at 03:41