package com.mkyong.test;
public class Main {
public static void main(String[] args) {
String something = "";
callSomething(something);
System.out.println(something);
}
private static String callSomething(String something) {
something = "Hello Wrold !";
return something;
}
}
Asked
Active
Viewed 36 times
-1
Konstantin Yovkov
- 60,548
- 8
- 97
- 143
Azhar Mohamed
- 355
- 3
- 10
-
1change callSomething(something); to something=callSomething(something); – pwwpche Jan 21 '15 at 07:44
-
another relevant question: http://stackoverflow.com/questions/1270760/passing-a-string-by-reference-in-java – default locale Jan 21 '15 at 07:45
-
String objects are immutable – EDToaster Jan 21 '15 at 07:49
1 Answers
0
No, in the method, you are changing the reference of the local variable something.
Change your method call to:
something = callSomething(something);
Stultuske
- 8,840
- 1
- 22
- 34