1 class Person{ 2 private int age; 3 public void setAge(int para){ 4 age = para; 5 } 6 public int getAge(){ 7 return age; 8 } 9 } 10 public static void main(String args[]){ 11 Person p = new Person(); 12 Person q; 13 p.setAge(10); 14 ArrayList<Person> students = new ArrayList<Person>(); 15 students.add(p); 16 q = students.get(0); 17 q.setAge(20); 18 System.out.println(students.get(0).getAge());//输出结果为20,因为q未初始化分配内存,对q赋值时,q与students.get(0)使用的是同一块内存,修改q,即修改students 19 }