Java. Why can't you change the reference to an object in a method?


My teacher asked me to answer a number of questions. And two of them really confuse me.

  1. Why can't you change the reference to an object in a method?

I cannot understand the meaning of the question (((

  1. How are parameters passed to methods in Java?

I googled this question and realized that it was always by value, but I didn’t understand exactly what it meant.

Could you help me to understand this situation.


As Vlad from Moscow hinted in the commentary to the question, the question is rather strange, and in my opinion it is not entirely correct – either your teacher is not very good at expressing his thoughts, or you somehow slightly distorted his question.

The point is that you can actually change the link in the method. Strictly speaking, in Jave they usually talk not about object references, but about variables (fields, arguments) of the object type. But in fact, the value of such variables is object references. Methods receive copies of these references as their arguments and can do whatever they want with them (that is, assign them the values ​​of references to other objects or null – "an empty reference"), which in no way affects the values ​​of the variables that were passed to the method during calling it. So here is the code

 public class TestQQ {
static void qq(String s) { System.out.println(s); s = "Что-то новое..."; // Это ссылка или не ссылка меняется ? System.out.println(s); }
public static void main(String[] args) { String string = "Нельзя менять ссылки?"; qq(string); System.out.println(string); } }

  1. Creates a new object of class String with the value "Can't change references?";
  2. Assigns a reference to it to the string variable;
  3. Calls the qq () method and passes it a copy of the string variable (that is, creates a second reference to the same "Can't change links?"
  4. The qq () method prints to the console the contents of the object referenced in its parameter – that is, the same "cannot be?";
  5. The qq () method assigns a new value to its parameter s – a link to a new string "Something new …" still contains a link to the same original "can't you?";
  6. The qq () method prints a new line ("Something new …") and returns control to the main program (main method);
  7. The main () method prints the contents of the object that is contained (that is, the reference to which is contained) in the variable string – this is still the original string "Can't change links?"

The output will be like this:

 Нельзя менять ссылки? Что-то новое Нельзя менять ссылки?

But at the same time, one must clearly understand that although the method cannot change the value of the object variable passed to it as a parameter (that is, a reference to the object), it can change the object itself for a sweet heart! For example, in such a program

 import java.util.ArrayList;
public class TestQQQ { static void qqq(ArrayList list) { list.add("можно."); }
public static void main(String[] args) { ArrayList strings = new ArrayList(); strings.add("А объекты менять...?"); qqq(strings); System.out.println(strings); } }

the qqq () method adds to the list, the link to which it received, a new line "can.", and as a result, we get the following at the output:
[А объекты менять...?, можно.]

Scroll to Top