-1

Below is my demo program:

public class Demo {
    public static void main(String[] args){
        Demo d = new Demo();
        Calendar c = Calendar.getInstance();
        System.out.println("Initial : "+c.getTime());
        d.addDay1(c);
        System.out.println("After addDay1 call : "+c.getTime());

    }

    public void addDay1(Calendar d){
        d.add(Calendar.DATE, 1);
    }

}

The output I am getting is the following :

Initial : Thu Mar 21 05:20:53 IST 2013
After addDay1 call : Fri Mar 22 05:20:53 IST 2013

But I am expecting the output as :

Initial : Thu Mar 21 05:20:53 IST 2013
After addDay1 call : Thu Mar 21 05:20:53 IST 2013

Why after my method call addDay1, the calendar object c has the added day in the main method?

In java, the parameters are passes by value in java, so why it is behaving indifferently?

Can anyone help me understand this?

Abubakkar
  • 15,488
  • 8
  • 55
  • 83

1 Answers1

2

You are passing the reference of Calendar hence adding day to it is reflected in the calling method.

Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33