如果我们创建一个 Immutable 类,如下所示以确保 Student 对象的状态的正确性,那么有没有办法像我们为 String 一样创建一个新对象,而无需像这种情况下调用 getInstance 方法?
public class Student {
private final StringBuffer name;
private final int roll;
private static Student student;
private Student(StringBuffer name , int roll)
{
this.name = name;
this.roll = roll;
}
public static Student getInstance(StringBuffer name,int roll)
{
student = new Student(name, roll);
return student;
}
}