2

I have asked this question here. I will try to make this one more specific.

class Example {

    public static void main(String[] args) {
        A a = null;
        load(a);
        System.out.println(a.toString());
        // outcome is null pointer exception
    }

    private static void load(A a) {
        a = new A();
    }

}

class A {
   public void String toString() {
       return "Hello, world!"
   }
}

So, does it possible to update a reference in a method? For some reason I need to do this. The reasons can be seen at above linked page.

Community
  • 1
  • 1
bijiDango
  • 1,385
  • 3
  • 14
  • 28

5 Answers5

2

Yes, it's possible if you define the parameter as A[] i.e. load(A[] a) and then in the method you update the element at position 0 in that array i.e. a[0] = new A(). Otherwise, it's not possible as Java is pass by value. I often use this workaround.

EXAMPLE 1:

class Example {

    public static void main(String[] args) {
        A[] a = new A[1];
        a[0] = new A("outer");
        System.out.println(a[0].toString());
        load(a);
        System.out.println(a[0].toString());
    }

    private static void load(A[] a) {
        a[0] = new A("inner");
    }

}

class A {

    private String name;

    public A(String nm){
        name = nm;
    }

    public String toString() {
        return "My name is: " + name;
    }
}

EXAMPLE 2:

class Example {

    public static void main(String[] args) {
        A[] a = new A[1];
        a[0] = null; // not needed, it is null anyway 
        load(a);
        System.out.println(a[0].toString());
    }

    private static void load(A[] a) {
        a[0] = new A("inner");
    }

}

class A {

    private String name;

    public A(String nm){
        name = nm;
    }

    public String toString() {
        return "My name is: " + name;
    }
}

NOTE: In fact, instead of an A[] you can use any wrapper object (an object which contains in itself a reference to an A object). The A[] a is just one such example. In this case a[0] is that reference to an A object. I just think that using an A[] is the easiest (most straightforward) way of achieving this.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
1

As already pointed by other java is pass-by-value.You need something like pointer in C with the object location address so that you can modify that particular address value.As an alternate to pointer you can use array.Example

class Example {

    public static void main(String[] args) {

        A[] aArray=new A[1];

        load(aArray);
        System.out.println(aArray[0].toString());
        // outcome is Hello, world!
    }

    private static void load(A[] aArray2) {
        aArray2[0] = new A();
    }

}

class A {
   public String toString() {
       return "Hello, world!";
   }
}
singhakash
  • 7,891
  • 6
  • 31
  • 65
0

You could just have:

public static void main(String[] args) {
    A a = load();
}

private static A load() {
    return new A();
}
Oli
  • 919
  • 8
  • 16
0

No you can't.

In java everything is passed as value not as reference.

user902383
  • 8,420
  • 8
  • 43
  • 63
  • Not quite true, everything in Java is passed by value as a reference. – Boris the Spider Mar 13 '15 at 10:42
  • Will [this](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) do? _Java is always pass-by-value. The difficult thing to understand is that Java passes objects as references and those references are passed by value._ – Boris the Spider Mar 13 '15 at 10:45
0

I came out with this. Perfectly satisfied my need and looks nice.

class A {

    private A reference;

    private String name;

    public A() {
        reference = this;
    }

    public void setReference(A ref) {
        reference = ref;
    }

    public void setName(String name) {
        reference.name = name;
    }

    public String getName() {
        return reference.name;
    }

}
bijiDango
  • 1,385
  • 3
  • 14
  • 28