package com.wq.test; public class Single { private String pname; private static Single onlyone = new Single(); private Single() { } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public static Single getOnlyone() { return onlyone; }; } package com.wq.test; public class Main { public static void m1() { Single s1 = Single.getOnlyone(); System.out.println(s1.getPname()); } public static void main(String[] args) { Single s1 = Single.getOnlyone(); s1.setPname("WQ"); System.out.println(s1.getPname()); m1(); } } 对象S1 S2都是操作同一个对象。