0

info.name and info.phone are changing after passing to defaultEntry(Info infoObject) but reference not changing to null why? can anybody please explain this scenario as well as working of java methods. Is there any difference between c functions and java methods working mechanism? Thanks in advance :)

   class Info {
        String name = "Sagar Pudi";
        String phone = "9999999999";
    }

    public class MemberDetails {
        public void defaultEntry(Info infoObject) {
            infoObject.name = "DEFAULT_MEMBER";
            infoObject.phone = "NO_PHONE";
            infoObject = null;
        }

        public static void main(String[] args) {

            Info info = new Info();
            System.out.println("Before : " + info.name + "<->" + info.phone);

            MemberDetails md = new MemberDetails();
            md.defaultEntry(info);
            System.out.println("After  : " + info.name + "<->" + info.phone);
            if (info != null)
            System.out.println("info  is still an object of " + info.getClass());
        }
    }

Output:

Before : Sagar Pudi<->9999999999
After  : DEFAULT_MEMBER<->NO_PHONE
info  is still an object of class Info
Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51

4 Answers4

0

Java's references are passed by value. i.e., the reference itself is copied.

So whatever changes you do to the passed reference (not the object the reference refers to) will not be reflected on the original object reference.

Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
Thihara
  • 7,031
  • 2
  • 29
  • 56
  • 1
    In other words, the `infoObject` variable within `defaultEntry` is a *different reference* to (initially) the same object. The method can see and change the object itself, but it can't see or change the caller's reference to the object. – Wyzard Oct 28 '14 at 04:39
0

Take a look at the AtomicReference<T> class wrapper. This is what you'll want. When modifying an object from within a function you cannot directly null the reference (you are essentially working with a copy of a pointer to the object, so nulling that will not do a thing to the caller of your method).

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicReference.html

    public void defaultEntry(Info infoObject) {
        infoObject.name = "DEFAULT_MEMBER";
        infoObject.phone = "NO_PHONE";
        infoObject = null;
    }

becomes

    public void defaultEntry(AtomicReference<Info> atomicInfoObject) {
        atomicInfoObject.get().name = "DEFAULT_MEMBER";
        atomicInfoObject.get().phone = "NO_PHONE";
        atomicInfoObject.set(null);
    }

On that same note, the mutability of arrays and lists also allows for abuse you could do the same with

public void defaultEntry(Info[] infoObject) {
    infoObject[0].name = "DEFAULT_MEMBER";
    infoObject[0].phone = "NO_PHONE";
    infoObject[0] = null;
}
Mike McMahon
  • 7,096
  • 3
  • 30
  • 42
0

Here you have two references(infoObject,info) for one object(Info),because java references are pass by value.

Making a reference null means,it does not point to any object. So in your case if you assign null to infoObject variable, the info variable will still continue holding references to Info class object.

dReAmEr
  • 6,986
  • 7
  • 36
  • 63
0

There is no "Pass by reference" concept in java. References also copied using "Pass by value" strategy. Here in the above case, variable "info" (reference value) is copied to "infoObject" in the called method. So while method body is executing (from the calling to the return from the function), we have two reference variables "info" and "infoObject" ideally pointing to the same data object in memory. So if we made change to any of the object data, it get reflected in both the objects. when you made

    infoObject = null;

it just makes that reference variable to be de-reference. Still variable "info" holds the valid object.

HJK
  • 1,382
  • 2
  • 9
  • 19