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



  • My teacher asked me to answer some questions. And two of them embarrass me.

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

    I can't understand the point.

    1. How do Java transfer parameters to methods?

    I'd googled that question, and I realized that I always meant it, but what exactly did that mean?

    Could you help me figure this out?



  • As Vlad from Moscow commented on the question, the question is rather strange, and in my view it is not quite correct -- either your teacher is not very good at expressing his mind, or you somehow misrepresented his question.

    The point is, the method is to change the reference actually. Strictly speaking, in Jave, it is usually not the references to the object, but the variables (fields, arguments) of an objective type. But the actual significance of such variables is the references to objects. Methods as their arguments are provided with copies of these references and can do what they want (i.e. assigning them the meaning of references to other objects or null - the empty link), which does not affect the variables that were transmitted to the method when it is called. So that's 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 the String class with the meaning of "No change of reference?"
    2. Establishes the reference to it by variable string;
    3. qqq() and sends him a copy of the variable string (i.e. creates a second reference to the same line, "Do not change references?"
    4. qq() isolates the contents of the object referred to in its parameter, i.e. the same " tree " ;
    5. The qqq() method for its parameter s attributes a new meaning - the reference to the new line, the second reference to the nudity disappears, but the reference variable string in the main programme does not change, and still contains a reference to the same original tree?
    6. qq() prints a new line (Some new...") and returns control to the main program.
    7. Method main(s) prints the contents of the object, which is contained (i.e. reference to) in the variable string, is still the original line, "Not to change references?"

    The conclusion will be:

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

    But it must be clearly understood that while the method cannot change the meaning of the objective variable given to it as a parameter (i.e. reference to the object), it can change the object for a nice soul! For example, in such a programme

    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); } }
    qqqq() adds to the list that he received, a new line "possible." and as a result, we get out here that:
    [А объекты менять...?, можно.]




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2