Java The state of an object does not change after a method call.

Beginner java question, but I canโ€™t understand how call-by-value (or Reference) works in the example below -

Why doesn't the String value change after it exits the method while my own String object is.? Same thing with other classes like Date ..

public class StringMadness { public static void main(String[] args) { String s = "Native String"; CustomStringObject cs = new CustomStringObject(); System.out.println("Custom String Before: " + cs.str); hello(cs); System.out.println("Custom String After: " + cs.str); System.out.println("Native String Before: " + s); hello(s); System.out.println("Native String After: " + s); } private static void hello(String t) { t = "hello " + t; } private static void hello(CustomStringObject o) { o.str = "hello " + o.str; } } class CustomStringObject { String str = "Custom String"; } 
+7
source share
5 answers

Compare these two methods:

 private static void hello(String t) { t = "hello " + t; } private static void hello(CustomStringObject o) { o.str = "hello " + o.str; } 

In the first case, you assign a new value to t . This will not affect the call code - you simply change the value of the parameter, and all arguments are passed by value in Java.

In the second case, you assign a new value to o.str . This is a change in the value of the field inside the object referenced by the value o . The caller will invoke this change because the caller still has a reference to this object.

In short: Java always uses pass by value, but you need to remember that for classes the value of a variable (or even any other expression) is a reference, not an object. You do not need to use parameter passing to see this:

 Foo foo1 = new Foo(); Foo foo2 = foo1; foo1.someField = "changed"; System.out.println(foo2.someField) // "changed" 

The second line here copies the value of foo1 to foo2 - these two variables refer to the same object, so it does not matter which variable you use to access it.

+16
source

There is an important difference between the two methods: using hello(String) , you try to change the reference to String , and using hello(CustomObject) , given the link, you use the link to change the member of the object.

hello(String) accepts a reference to String . In the function you are trying to change, to the referenced object, but you only change the help copy in order. Thus, your changes are not reflected outside the method.

hello(CustomObject) provides a copy of the object link that you can use to modify the actual object. Think of it as changing the contents of an object. Thus, your changes are reflected in the caller.

Given a reference to an object, you can modify the object using its public methods / fields

+4
source

t will point to the new object and scope only for the method, so the changes are not visible from the outside.

The second case, the value that you change will be updated to the object, so these changes will be visible after the method is called.

0
source

Because for String, you just change the local parameter.

0
source

Doesn't work because String is an immutable object

0
source

All Articles