-2

I have written a code -

// Node Class
class aNode {

    // Node Contents
    int NodeInt;
    char NodeChar;

    // constructor
    aNode() {
    }

    aNode(int x, char y) {
        NodeInt = x;
        NodeChar = y;
    }
}

class MainClass {

    static aNode node = new aNode();

    public static void main(String[] args) {

        node = null;
        function(node);

        if (node == null) {
            System.out.println("Node is null");
        }
    }

    static void function(aNode x) {
        if (x == null) {
            System.out.println("Node is null");
        }

        x = new aNode(5, 'c');

        System.out.println(x.NodeInt);
        System.out.println(x.NodeChar);
    }
}

I expected the output to be -

Node is null
5
c

but when the program returns to main, the value of node is set to null again. So I get the output to be -

Node is null
5
c
Node is null 

Please help me modify the code to get desired output. Any help will be appreciated!

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Take a look at [is-java-pass-by-reference](http://stackoverflow.com/questions/40480/is-java-pass-by-reference?rq=1) – Pshemo Apr 01 '13 at 10:00

5 Answers5

3

You should know, that aNode node and aNode x are references to different objects. It is one of Java features - passing only by value. It mean, that when you are calling

function(node);

you are not passing node reference to method function(...), you are creating new reference to the same object. But in line

x = new aNode(5,'c');

you are setting reference x to new object. So, node still references to null and x references to new aNode.

To get more about passing params in Java, read next article.

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.

bsiamionau
  • 8,099
  • 4
  • 46
  • 73
  • that helped !! Now I used a return statement in functions that over-rides the value in main and it's fixed. Thanks for help :) – shivamtiwari93 Apr 01 '13 at 10:08
0

You are passing a static object, but in the method function(), you are not changing the value of object node. You are changing the value of other object only. So in main, the value of node s null only.

iCode
  • 8,892
  • 21
  • 57
  • 91
0

In general, this is because Java passes a copy of the reference to the aNode object to your method. Changing this reference will not change the original reference.

NilsH
  • 13,705
  • 4
  • 41
  • 59
0

Inside function(), x is simply a local variable. When you reassign a reference in Java, you are modifying the content of the reference itself, not the one of the referred object. There's no way in Java to pass the address-of an object. If you want such a behavior, you can try with a generic class like Wrapper

Raffaele
  • 20,627
  • 6
  • 47
  • 86
0

Real pass-by-reference is impossible in Java. Java passes everything by value, including references.. So you have to slightly change your code to get the desired output:

class aNode{

    //Node Contents
    int NodeInt;
    char NodeChar;

    //constructor
    aNode(){
    }
    aNode(int x, char y){
        NodeInt = x;
        NodeChar = y;
    }
}

class JavaApplication8{

    static aNode node = new aNode();

    public static void main(String[] args){


        node = null;
        node=function(node);

        if(node == null){
            System.out.println("Node is null");
        }
    }

    static aNode function(aNode x){
        if(x == null)
        {
            System.out.println("Node is null");
        }

        x = new aNode(5,'c');

        System.out.println(x.NodeInt);
        System.out.println(x.NodeChar);
        return x;
    }
}

Output:

Node is null
5
c
Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71