Confused about the reference in Java











up vote
1
down vote

favorite












I am confused about why the value of next is not the same as the root.children[0]? In my understanding, next points to root.children[0]. Therefore, if the value of root.children[0] is changed, next should also change.



public class MyClass {
public static void main(String args) {
Node root = new Node();
Node next = root.children[0];
root.children[0] = new Node();
System.out.println(root.children[0]);
System.out.println(next);
}

public static class Node {
Node children = new Node[1];
}
}


output



MyClass$Node@e6ea0c6
null









share|improve this question




















  • 1




    next does not point to root.children[0], it's a copy of root.children[0] and therefore references the same object as root.children[0] for as long as root.children[0] is not changed.
    – Thomas Kläger
    Nov 7 at 7:51










  • When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same
    – Pushpesh Kumar Rajwanshi
    Nov 7 at 7:51










  • you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line
    – Stultuske
    Nov 7 at 7:52










  • This is very bad code!! Unless it is some sort of programming challenge, you should consider rewriting it. If that's the case (you would like a rewrite), Im happy to add an answer for you. Real world java code should never look like this code as it stands, however...
    – vikingsteve
    Nov 7 at 8:02










  • @vikingsteve Yes, this is an example from the coding challenge. I face the problem when trying to build a Trie structure.
    – LoveTW
    Nov 7 at 8:05















up vote
1
down vote

favorite












I am confused about why the value of next is not the same as the root.children[0]? In my understanding, next points to root.children[0]. Therefore, if the value of root.children[0] is changed, next should also change.



public class MyClass {
public static void main(String args) {
Node root = new Node();
Node next = root.children[0];
root.children[0] = new Node();
System.out.println(root.children[0]);
System.out.println(next);
}

public static class Node {
Node children = new Node[1];
}
}


output



MyClass$Node@e6ea0c6
null









share|improve this question




















  • 1




    next does not point to root.children[0], it's a copy of root.children[0] and therefore references the same object as root.children[0] for as long as root.children[0] is not changed.
    – Thomas Kläger
    Nov 7 at 7:51










  • When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same
    – Pushpesh Kumar Rajwanshi
    Nov 7 at 7:51










  • you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line
    – Stultuske
    Nov 7 at 7:52










  • This is very bad code!! Unless it is some sort of programming challenge, you should consider rewriting it. If that's the case (you would like a rewrite), Im happy to add an answer for you. Real world java code should never look like this code as it stands, however...
    – vikingsteve
    Nov 7 at 8:02










  • @vikingsteve Yes, this is an example from the coding challenge. I face the problem when trying to build a Trie structure.
    – LoveTW
    Nov 7 at 8:05













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am confused about why the value of next is not the same as the root.children[0]? In my understanding, next points to root.children[0]. Therefore, if the value of root.children[0] is changed, next should also change.



public class MyClass {
public static void main(String args) {
Node root = new Node();
Node next = root.children[0];
root.children[0] = new Node();
System.out.println(root.children[0]);
System.out.println(next);
}

public static class Node {
Node children = new Node[1];
}
}


output



MyClass$Node@e6ea0c6
null









share|improve this question















I am confused about why the value of next is not the same as the root.children[0]? In my understanding, next points to root.children[0]. Therefore, if the value of root.children[0] is changed, next should also change.



public class MyClass {
public static void main(String args) {
Node root = new Node();
Node next = root.children[0];
root.children[0] = new Node();
System.out.println(root.children[0]);
System.out.println(next);
}

public static class Node {
Node children = new Node[1];
}
}


output



MyClass$Node@e6ea0c6
null






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 8:18









Mark Jeronimus

4,76421836




4,76421836










asked Nov 7 at 7:47









LoveTW

1,46683045




1,46683045








  • 1




    next does not point to root.children[0], it's a copy of root.children[0] and therefore references the same object as root.children[0] for as long as root.children[0] is not changed.
    – Thomas Kläger
    Nov 7 at 7:51










  • When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same
    – Pushpesh Kumar Rajwanshi
    Nov 7 at 7:51










  • you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line
    – Stultuske
    Nov 7 at 7:52










  • This is very bad code!! Unless it is some sort of programming challenge, you should consider rewriting it. If that's the case (you would like a rewrite), Im happy to add an answer for you. Real world java code should never look like this code as it stands, however...
    – vikingsteve
    Nov 7 at 8:02










  • @vikingsteve Yes, this is an example from the coding challenge. I face the problem when trying to build a Trie structure.
    – LoveTW
    Nov 7 at 8:05














  • 1




    next does not point to root.children[0], it's a copy of root.children[0] and therefore references the same object as root.children[0] for as long as root.children[0] is not changed.
    – Thomas Kläger
    Nov 7 at 7:51










  • When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same
    – Pushpesh Kumar Rajwanshi
    Nov 7 at 7:51










  • you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line
    – Stultuske
    Nov 7 at 7:52










  • This is very bad code!! Unless it is some sort of programming challenge, you should consider rewriting it. If that's the case (you would like a rewrite), Im happy to add an answer for you. Real world java code should never look like this code as it stands, however...
    – vikingsteve
    Nov 7 at 8:02










  • @vikingsteve Yes, this is an example from the coding challenge. I face the problem when trying to build a Trie structure.
    – LoveTW
    Nov 7 at 8:05








1




1




next does not point to root.children[0], it's a copy of root.children[0] and therefore references the same object as root.children[0] for as long as root.children[0] is not changed.
– Thomas Kläger
Nov 7 at 7:51




next does not point to root.children[0], it's a copy of root.children[0] and therefore references the same object as root.children[0] for as long as root.children[0] is not changed.
– Thomas Kläger
Nov 7 at 7:51












When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same
– Pushpesh Kumar Rajwanshi
Nov 7 at 7:51




When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same
– Pushpesh Kumar Rajwanshi
Nov 7 at 7:51












you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line
– Stultuske
Nov 7 at 7:52




you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line
– Stultuske
Nov 7 at 7:52












This is very bad code!! Unless it is some sort of programming challenge, you should consider rewriting it. If that's the case (you would like a rewrite), Im happy to add an answer for you. Real world java code should never look like this code as it stands, however...
– vikingsteve
Nov 7 at 8:02




This is very bad code!! Unless it is some sort of programming challenge, you should consider rewriting it. If that's the case (you would like a rewrite), Im happy to add an answer for you. Real world java code should never look like this code as it stands, however...
– vikingsteve
Nov 7 at 8:02












@vikingsteve Yes, this is an example from the coding challenge. I face the problem when trying to build a Trie structure.
– LoveTW
Nov 7 at 8:05




@vikingsteve Yes, this is an example from the coding challenge. I face the problem when trying to build a Trie structure.
– LoveTW
Nov 7 at 8:05












3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










Consider it like this, I'll mark the object in memory as {} and the reference as ->
So you start by next = root.children[0], at this time root.children[0] -> null, it points to nothing in memory, no object, so next -> null.



Then you do root.children[0] -> {a new Node} but next is still next -> null it doesn't point to the same object, it's not a shortcut to root.children[0], it's NOT next -> root.children[0] -> {a new Node}, next points to nothing



If you had root.children[0] -> {a new Node}, and then do next = root.children[0], then next would point next -> {a new Node}, but again if your do now root.children[0] = new Node() it will result in root.children[0] -> {a newer Node}
and next will NOT point to this newer node



When you assign a object's reference to a variable, that variable will not always point to the same address in memory, by doing new Node() you create a new object somewhere in memory and with = you tell a variable to point to that newly allocated object






share|improve this answer





















  • So... the output of the program is something like Node@a153fb40 and then null, right ?
    – vikingsteve
    Nov 7 at 8:18










  • Sorry, which program are you referring to?
    – Blagoj Atanasovski
    Nov 7 at 9:37










  • of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
    – vikingsteve
    Nov 7 at 10:38




















up vote
4
down vote













Let's dissect this code line by line:



Node root = new Node();


You created a new Node object. This object has a children array of length 1. Since you have not assigned anything to children yet, the array contains the single element null.



Node next = root.children[0];


As I said, children[0] is null, so next is now null. Note that in this line, you did not make it so that next always points to the same thing as children[0]. You only made next point to the same thing as children[0] is pointing to at that time.



root.children[0] = new Node();


Now children[0] is being assigned a non-null value. Note that this does not change the value of next.






share|improve this answer





















  • So next is still null at the end of execution, right?
    – vikingsteve
    Nov 7 at 8:15










  • @vikingsteve Yes. The value of a variable doesn't change until the programmer changes it. next is only set once (to null), and it will stay that way.
    – Sweeper
    Nov 7 at 8:17










  • I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between using root.children[0] in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(
    – LoveTW
    Nov 7 at 8:18










  • @LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
    – Sweeper
    Nov 7 at 8:20










  • @LoveTW i think both answers deserve an upvote. +1 from me :)
    – vikingsteve
    Nov 7 at 8:40


















up vote
0
down vote













Currently, root.children[0] just have reference not the object. So, You need to First add children to root node and then assign as I have changed into below code.



public class MyClass {
public static void main(String args) {
Node root = new Node();
root.children[0] = new Node();
Node next = root.children[0];
System.out.println(root.children[0]);
System.out.println(next);
}

public static class Node {
Node children = new Node[1];
}
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185325%2fconfused-about-the-reference-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    Consider it like this, I'll mark the object in memory as {} and the reference as ->
    So you start by next = root.children[0], at this time root.children[0] -> null, it points to nothing in memory, no object, so next -> null.



    Then you do root.children[0] -> {a new Node} but next is still next -> null it doesn't point to the same object, it's not a shortcut to root.children[0], it's NOT next -> root.children[0] -> {a new Node}, next points to nothing



    If you had root.children[0] -> {a new Node}, and then do next = root.children[0], then next would point next -> {a new Node}, but again if your do now root.children[0] = new Node() it will result in root.children[0] -> {a newer Node}
    and next will NOT point to this newer node



    When you assign a object's reference to a variable, that variable will not always point to the same address in memory, by doing new Node() you create a new object somewhere in memory and with = you tell a variable to point to that newly allocated object






    share|improve this answer





















    • So... the output of the program is something like Node@a153fb40 and then null, right ?
      – vikingsteve
      Nov 7 at 8:18










    • Sorry, which program are you referring to?
      – Blagoj Atanasovski
      Nov 7 at 9:37










    • of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
      – vikingsteve
      Nov 7 at 10:38

















    up vote
    3
    down vote



    accepted










    Consider it like this, I'll mark the object in memory as {} and the reference as ->
    So you start by next = root.children[0], at this time root.children[0] -> null, it points to nothing in memory, no object, so next -> null.



    Then you do root.children[0] -> {a new Node} but next is still next -> null it doesn't point to the same object, it's not a shortcut to root.children[0], it's NOT next -> root.children[0] -> {a new Node}, next points to nothing



    If you had root.children[0] -> {a new Node}, and then do next = root.children[0], then next would point next -> {a new Node}, but again if your do now root.children[0] = new Node() it will result in root.children[0] -> {a newer Node}
    and next will NOT point to this newer node



    When you assign a object's reference to a variable, that variable will not always point to the same address in memory, by doing new Node() you create a new object somewhere in memory and with = you tell a variable to point to that newly allocated object






    share|improve this answer





















    • So... the output of the program is something like Node@a153fb40 and then null, right ?
      – vikingsteve
      Nov 7 at 8:18










    • Sorry, which program are you referring to?
      – Blagoj Atanasovski
      Nov 7 at 9:37










    • of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
      – vikingsteve
      Nov 7 at 10:38















    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    Consider it like this, I'll mark the object in memory as {} and the reference as ->
    So you start by next = root.children[0], at this time root.children[0] -> null, it points to nothing in memory, no object, so next -> null.



    Then you do root.children[0] -> {a new Node} but next is still next -> null it doesn't point to the same object, it's not a shortcut to root.children[0], it's NOT next -> root.children[0] -> {a new Node}, next points to nothing



    If you had root.children[0] -> {a new Node}, and then do next = root.children[0], then next would point next -> {a new Node}, but again if your do now root.children[0] = new Node() it will result in root.children[0] -> {a newer Node}
    and next will NOT point to this newer node



    When you assign a object's reference to a variable, that variable will not always point to the same address in memory, by doing new Node() you create a new object somewhere in memory and with = you tell a variable to point to that newly allocated object






    share|improve this answer












    Consider it like this, I'll mark the object in memory as {} and the reference as ->
    So you start by next = root.children[0], at this time root.children[0] -> null, it points to nothing in memory, no object, so next -> null.



    Then you do root.children[0] -> {a new Node} but next is still next -> null it doesn't point to the same object, it's not a shortcut to root.children[0], it's NOT next -> root.children[0] -> {a new Node}, next points to nothing



    If you had root.children[0] -> {a new Node}, and then do next = root.children[0], then next would point next -> {a new Node}, but again if your do now root.children[0] = new Node() it will result in root.children[0] -> {a newer Node}
    and next will NOT point to this newer node



    When you assign a object's reference to a variable, that variable will not always point to the same address in memory, by doing new Node() you create a new object somewhere in memory and with = you tell a variable to point to that newly allocated object







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 7 at 7:53









    Blagoj Atanasovski

    320111




    320111












    • So... the output of the program is something like Node@a153fb40 and then null, right ?
      – vikingsteve
      Nov 7 at 8:18










    • Sorry, which program are you referring to?
      – Blagoj Atanasovski
      Nov 7 at 9:37










    • of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
      – vikingsteve
      Nov 7 at 10:38




















    • So... the output of the program is something like Node@a153fb40 and then null, right ?
      – vikingsteve
      Nov 7 at 8:18










    • Sorry, which program are you referring to?
      – Blagoj Atanasovski
      Nov 7 at 9:37










    • of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
      – vikingsteve
      Nov 7 at 10:38


















    So... the output of the program is something like Node@a153fb40 and then null, right ?
    – vikingsteve
    Nov 7 at 8:18




    So... the output of the program is something like Node@a153fb40 and then null, right ?
    – vikingsteve
    Nov 7 at 8:18












    Sorry, which program are you referring to?
    – Blagoj Atanasovski
    Nov 7 at 9:37




    Sorry, which program are you referring to?
    – Blagoj Atanasovski
    Nov 7 at 9:37












    of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
    – vikingsteve
    Nov 7 at 10:38






    of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
    – vikingsteve
    Nov 7 at 10:38














    up vote
    4
    down vote













    Let's dissect this code line by line:



    Node root = new Node();


    You created a new Node object. This object has a children array of length 1. Since you have not assigned anything to children yet, the array contains the single element null.



    Node next = root.children[0];


    As I said, children[0] is null, so next is now null. Note that in this line, you did not make it so that next always points to the same thing as children[0]. You only made next point to the same thing as children[0] is pointing to at that time.



    root.children[0] = new Node();


    Now children[0] is being assigned a non-null value. Note that this does not change the value of next.






    share|improve this answer





















    • So next is still null at the end of execution, right?
      – vikingsteve
      Nov 7 at 8:15










    • @vikingsteve Yes. The value of a variable doesn't change until the programmer changes it. next is only set once (to null), and it will stay that way.
      – Sweeper
      Nov 7 at 8:17










    • I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between using root.children[0] in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(
      – LoveTW
      Nov 7 at 8:18










    • @LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
      – Sweeper
      Nov 7 at 8:20










    • @LoveTW i think both answers deserve an upvote. +1 from me :)
      – vikingsteve
      Nov 7 at 8:40















    up vote
    4
    down vote













    Let's dissect this code line by line:



    Node root = new Node();


    You created a new Node object. This object has a children array of length 1. Since you have not assigned anything to children yet, the array contains the single element null.



    Node next = root.children[0];


    As I said, children[0] is null, so next is now null. Note that in this line, you did not make it so that next always points to the same thing as children[0]. You only made next point to the same thing as children[0] is pointing to at that time.



    root.children[0] = new Node();


    Now children[0] is being assigned a non-null value. Note that this does not change the value of next.






    share|improve this answer





















    • So next is still null at the end of execution, right?
      – vikingsteve
      Nov 7 at 8:15










    • @vikingsteve Yes. The value of a variable doesn't change until the programmer changes it. next is only set once (to null), and it will stay that way.
      – Sweeper
      Nov 7 at 8:17










    • I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between using root.children[0] in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(
      – LoveTW
      Nov 7 at 8:18










    • @LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
      – Sweeper
      Nov 7 at 8:20










    • @LoveTW i think both answers deserve an upvote. +1 from me :)
      – vikingsteve
      Nov 7 at 8:40













    up vote
    4
    down vote










    up vote
    4
    down vote









    Let's dissect this code line by line:



    Node root = new Node();


    You created a new Node object. This object has a children array of length 1. Since you have not assigned anything to children yet, the array contains the single element null.



    Node next = root.children[0];


    As I said, children[0] is null, so next is now null. Note that in this line, you did not make it so that next always points to the same thing as children[0]. You only made next point to the same thing as children[0] is pointing to at that time.



    root.children[0] = new Node();


    Now children[0] is being assigned a non-null value. Note that this does not change the value of next.






    share|improve this answer












    Let's dissect this code line by line:



    Node root = new Node();


    You created a new Node object. This object has a children array of length 1. Since you have not assigned anything to children yet, the array contains the single element null.



    Node next = root.children[0];


    As I said, children[0] is null, so next is now null. Note that in this line, you did not make it so that next always points to the same thing as children[0]. You only made next point to the same thing as children[0] is pointing to at that time.



    root.children[0] = new Node();


    Now children[0] is being assigned a non-null value. Note that this does not change the value of next.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 7 at 7:55









    Sweeper

    60k967134




    60k967134












    • So next is still null at the end of execution, right?
      – vikingsteve
      Nov 7 at 8:15










    • @vikingsteve Yes. The value of a variable doesn't change until the programmer changes it. next is only set once (to null), and it will stay that way.
      – Sweeper
      Nov 7 at 8:17










    • I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between using root.children[0] in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(
      – LoveTW
      Nov 7 at 8:18










    • @LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
      – Sweeper
      Nov 7 at 8:20










    • @LoveTW i think both answers deserve an upvote. +1 from me :)
      – vikingsteve
      Nov 7 at 8:40


















    • So next is still null at the end of execution, right?
      – vikingsteve
      Nov 7 at 8:15










    • @vikingsteve Yes. The value of a variable doesn't change until the programmer changes it. next is only set once (to null), and it will stay that way.
      – Sweeper
      Nov 7 at 8:17










    • I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between using root.children[0] in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(
      – LoveTW
      Nov 7 at 8:18










    • @LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
      – Sweeper
      Nov 7 at 8:20










    • @LoveTW i think both answers deserve an upvote. +1 from me :)
      – vikingsteve
      Nov 7 at 8:40
















    So next is still null at the end of execution, right?
    – vikingsteve
    Nov 7 at 8:15




    So next is still null at the end of execution, right?
    – vikingsteve
    Nov 7 at 8:15












    @vikingsteve Yes. The value of a variable doesn't change until the programmer changes it. next is only set once (to null), and it will stay that way.
    – Sweeper
    Nov 7 at 8:17




    @vikingsteve Yes. The value of a variable doesn't change until the programmer changes it. next is only set once (to null), and it will stay that way.
    – Sweeper
    Nov 7 at 8:17












    I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between using root.children[0] in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(
    – LoveTW
    Nov 7 at 8:18




    I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between using root.children[0] in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(
    – LoveTW
    Nov 7 at 8:18












    @LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
    – Sweeper
    Nov 7 at 8:20




    @LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
    – Sweeper
    Nov 7 at 8:20












    @LoveTW i think both answers deserve an upvote. +1 from me :)
    – vikingsteve
    Nov 7 at 8:40




    @LoveTW i think both answers deserve an upvote. +1 from me :)
    – vikingsteve
    Nov 7 at 8:40










    up vote
    0
    down vote













    Currently, root.children[0] just have reference not the object. So, You need to First add children to root node and then assign as I have changed into below code.



    public class MyClass {
    public static void main(String args) {
    Node root = new Node();
    root.children[0] = new Node();
    Node next = root.children[0];
    System.out.println(root.children[0]);
    System.out.println(next);
    }

    public static class Node {
    Node children = new Node[1];
    }
    }





    share|improve this answer

























      up vote
      0
      down vote













      Currently, root.children[0] just have reference not the object. So, You need to First add children to root node and then assign as I have changed into below code.



      public class MyClass {
      public static void main(String args) {
      Node root = new Node();
      root.children[0] = new Node();
      Node next = root.children[0];
      System.out.println(root.children[0]);
      System.out.println(next);
      }

      public static class Node {
      Node children = new Node[1];
      }
      }





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Currently, root.children[0] just have reference not the object. So, You need to First add children to root node and then assign as I have changed into below code.



        public class MyClass {
        public static void main(String args) {
        Node root = new Node();
        root.children[0] = new Node();
        Node next = root.children[0];
        System.out.println(root.children[0]);
        System.out.println(next);
        }

        public static class Node {
        Node children = new Node[1];
        }
        }





        share|improve this answer












        Currently, root.children[0] just have reference not the object. So, You need to First add children to root node and then assign as I have changed into below code.



        public class MyClass {
        public static void main(String args) {
        Node root = new Node();
        root.children[0] = new Node();
        Node next = root.children[0];
        System.out.println(root.children[0]);
        System.out.println(next);
        }

        public static class Node {
        Node children = new Node[1];
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 7:58









        Raheela Aslam

        3076




        3076






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185325%2fconfused-about-the-reference-in-java%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings