import java.util.*;
public class Practice {
/**
* @param args
*/
public static void main(
String[] args) {
Practice p = new Practice();
ArrayList a = p.get();
a.add(1);
System.out.println("in main" +a );
}
public ArrayList<String> get() {
ArrayList<String> a = new ArrayList<String>();
a.add("ss");
a.add("aa");
a.add("dd");
//a.add(2);
System.out.println("in " +a);
return a;
}
}
The problem is :
i am creating new ArrayList which will accept only String object in get method(). Return type of method is also ArrayList<String>.
But when method is called in main method (or any other method) and reference of ArrayList is given to that method,ArrayList reference is able to add all object here.
Whats the concept behind it..?