0

Please consider this short code :

class abs{}
class cd
{
    static void method(abs a)
    {
        System.out.println(a); // PRINTS NULL
        a = new abs();
        System.out.println(a); // PRINTS NEWLY GENERATED HASHCODE
    }
    public static void main(String...args)
    {
        abs gh = null;
        // REFERENCE SET TO NULL

        // NOW PASSING IT TO A METHOD
        method(gh);

        // IF OBJECT CALL IS CALL BY REFERNCE, THEN WHY DOES THIS PRINT NULL ?
        System.out.println(gh);
    }
}

My comments explain what I want. Basically the last print statement should print the hashcode but it prints 'null'. What is the reason behind this ?

Gagan93
  • 1,826
  • 2
  • 25
  • 38

4 Answers4

3

Java is pass by value. This is what happens in your code:

//gh initialized as null
abs gh = null;
//passing a copy of the value of the reference to the method
method(gh);
//gh keeps the value of the current reference which is null
System.out.println(gh);

A better example of this is when trying to replace an object obtained when iterating through a List:

List<String> stringList = Arrays.asList("hello", "world");
System.out.println(stringList);
for (String currentString : stringList) {
    //naively trying to change the contents from ["hello", "world"] to ["bye", "world"]
    if (currentString.equals("hello")) {
        currentString = "bye";
    }
}
System.out.println(stringList);

Output:

["hello", "world"]
["hello", "world"]

This is because the enhanced for loop will work with a copy of the reference provided by an iterator. The for loop code above can be translated to this:

for (Iterator<String> it = stringList.iterator(); it.hasNext(); ) {
    String currentString = it.next();
    //now, it's more clear that you're updating the variable currentString
    //that holds a copy of the reference stored in the List
    //this explains why this approach won't work
    if (currentString.equals("hello")) {
        currentString = "bye";
    }
}
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • but according to my knowledge, objects are passed by reference in java. Aren't they ? – Gagan93 Aug 08 '14 at 17:26
  • @Gagan93 no. You pass a copy of the value of the reference. That's why you can update the fields on the reference, but you cannot update the whole reference because you only receive a copy of it. – Luiggi Mendoza Aug 08 '14 at 17:27
  • @Gagan93 answer updated to show another example of pass-by-value. – Luiggi Mendoza Aug 08 '14 at 17:43
1

Java is a pass by value language. That's why your gh variable is not changed by the method call. If you want the instance created in method to be returned by method, have method return it :

class cd
{
    static abs method()
    {
        abs a = new abs();
        System.out.println(a);
        return abs;
    }
    public static void main(String...args)
    {
        abs gh = null;
        gh = method();
        System.out.println(gh);
    }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
1

In your code 'gh' and 'a' are two method local variables. By setting gh to null and sending it to static method named method! you are not passing any object reference to 'a'. Null doesnot indicate any object reference. So changes made to local variable 'a' is not reflected to 'gh'.

Manjunath
  • 1,685
  • 9
  • 9
0

You're having an Object of class abs. Then you pass it to your function. Till there is no problem (expect for it is null).

Now, in your function, you re-assign this variable. Inside your method, everything is fine.

Outside of your method, nobody recognized that the variable changed. So it is still null. That's why your code above shouldn't have compiled btw.

What you want is method() to return the object. Code:

static abs method ()
{
    return new abs();
}
public static void main (String[] args)
{
    abs gh = method();
    System.out.println(gh); // this won't print null
}
msrd0
  • 7,816
  • 9
  • 47
  • 82