To my best knowledge Java uses pass by value. but if you look below I passed a list and added a Integer to it.
Desired output : list before []
and after should be []
Resultant output : list before []
and after is [20]
Also,
if I initialize the list inside static function that my desired output is achieved .
Can i know the reason for this phenomenon please
import java.util.ArrayList;
import java.util.List;
public class NormalTest {
public static void testMethod(Integer testInt, List<Integer> sampleList) {
testInt *= 2;
System.out.println(" Inside testInt :: " + testInt);
sampleList.add(testInt);
}
public static void main(String[] args) {
Integer testInt = 10;
List<Integer> sampleList = new ArrayList<>();
System.out.println(" Before testInt :: " + testInt);
System.out.println(" Before sampleList :: " + sampleList);
NormalTest.testMethod(testInt, sampleList);
System.out.println(" After testInt :: " + testInt);
System.out.println(" After sampleList :: " + sampleList);
}
}