In the following code I added the ex1 object to the ArrayList. Then I made the ex1 object to null. But when I returned the object from the arraylist still it is pervious value not changed to null. Why is that? As i think all the reference variable refer to same object in the heap. So it should be null
List<Example> ex = new ArrayList<>();
Stack<Example> st = new Stack<>();
Example ex1 = new Example();
ex1.age = 15;
ex.add(ex1);
st.push(ex1);
ex1 = null;
//System.out.println(ex1.age);
System.out.println(ex.get(0).age);
System.out.println(st.peek().age);
Ouput is 15