I've got a doubt with the below code. any help will be appreciable.
class Test {
static void test(List<Object> obj) {
obj.add(5);
obj = new ArrayList<>();
}
public static void main(String[] args) {
List<Object> list = new LinkedList<>();
list.add(10);
System.out.println(list);
test(list);
System.out.println(list);
}
}
what will be the content of list object after test method returned to main method?
Expected Output:
[10] [] //empty
since we are creating a new arraylist object in the same list reference which is passed to test method from main method.
Actual Output :
[10] [10, 5]
could please explain this?