Ok bit of an odd question. I have the following code, which just creates a simple java object called DumObj and sets a string value using a setter method. Then a few methods are called from a TestBed class using the DumObj as a parameter.
I initially thought that calling TestBed.updateId(DumObj) would not affect my DumObj, and the initial value of ID that was set to "apple" would stay the same. (Because of the whole pass-by-value thing)
However the value of ID was set to the updated value of "orange". Ok I thought, that's weird, so I wrote another method, TestBed.setToNull(DumObj). This method just sets DumObj to null, so when I call the getId() method I was expecting to get a null pointer exception.
However the output I got was the value of ID still set to "orange".
Code is as follows :
    public static void main(String[] args) 
    {   
            TestBed test = new TestBed();
            DumObj one = new DumObj();
            one.setId("apple");
            System.out.println("Id : " + one.getId());
            test.updateId(one);
            System.out.println("Id : " + one.getId());
            test.setToNull(one);
            System.out.println("Id : " + one.getId());
    }
    public void updateId(DumObj two)
    {
            two.setId("orange");
    }
    public void setToNull(DumObj two)
    {
            two = null;
    }
Output is as follows :
    Id : apple
    Id : orange
    Id : orange
It's probably something really simple I'm overlooking, but can someone explain this behaviour to me? Is Java not pass-by-value?
 
     
     
     
    