0
    class Ideone
    {
      int x;
      public static void main (String[] args) throws java.lang.Exception
      {
         Ideone i;
         i = new Ideone();
         i.x = 25;
         System.out.println(i.x);
         f(i);
         System.out.println(i.x);
         g(i);
         System.out.println(i.x);
      }

      public static void f(Ideone j){
         j = new Ideone();
         j.x = 55; // It just changes new instance of J. It is not changing actual object
      }

      public static void j(Ideone j){
         j.x = 52; // It modifies actual object
      }
  }

I have a doubt in this. In case of j function, it modifies actual object. Is it not pass by reference? We are passing the object and modifying it inside j.

But I am doing the samein function f also. Passing the object and modifying it. But it is not modifying the original object.

I am confused the behaviour between these.

As per the highly voted answer, I am passing an URL in both cases. And I am modifying the content in the URL. But it is visible to all in one case but it is not in the other case.

Community
  • 1
  • 1
Gibbs
  • 21,904
  • 13
  • 74
  • 138

1 Answers1

2

The f method overwrites the reference j passed to it. Therefore it cannot change the state of the Ideone instance whose reference was passed to it from the caller. Note that the assignment j = new Ideone() does not affect the caller of f, since Java passes the value of the reference variable, not a reference to it.

The j method doesn't overwrite the reference, so it can modify the state of the instance referred by it.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Then can't we say java is using pass by reference? This is my doubt. – Gibbs Jan 07 '16 at 14:12
  • 1
    @GopsAB Object references are pass by value. – Tdorno Jan 07 '16 at 14:13
  • @GopsAB You are passing a value, but the value in this case is a reference to an object. – Eran Jan 07 '16 at 14:14
  • @Eran Thanks. So Java passes the state[values not the original object]. So In case of method f, I am creating new object of type Ideone. It will not affect the original object. After that I am modifying the new object. But in case of method j, I am modifying the state[values]. So I can change the values[state] of the object that I pass. Am I right? Please correct me – Gibbs Jan 07 '16 at 14:18
  • @GopsAB as stated in the linked duplicate, Java is always pass-by-value. When you pass an object, you actually pass the reference (or, to be more precise, the internal object-id) and, as Eran explained, the assignment of a new object to the parameter does only affect the called side, not the caller's side. You may want to draw the references and actual objects to clarify which reference is referencing which object. – Turing85 Jan 07 '16 at 14:22
  • @GopsAB Java passes values. Changing the values inside the method doesn't change the values that were passed to the method. However, if the value is a reference to an object, and that object has methods that modify its state (or public properties that can be directly assigned), the method can change the state of the object whose reference was passed to it. It can't, however, make that reference point to a different object (if it does, those changes are local to the method and don't affect the caller of the method). – Eran Jan 07 '16 at 14:22
  • @Eran, I read all the toppest answers in the actual question. But I am not able to understand/convinced why we say Java is pass by value. By the method f, I can say java is pass by reference. By the method J, I can say java is pass by value. So java is both by reference & value. More confused. – Gibbs Jan 11 '16 at 16:20