Can anyone tell me what is wrong with this code?
public abstract class BoardTestBean{
protected String month;
protected String day;
protected String name;
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public String getYear() {
return day;
}
public void setYear(String day) {
this.day = day;
}
public String getName(){
return name;
}
//Classes
public class SAT {
boolean pre2005=false;
private String verbal;
private String quantitative;
private String writing="";//if pre-2005, do not set. It is not used.
public SAT() {
super();
if(pre2005)
name="SAT (pre 2005)";
else
name="SAT";
}
public SAT(String verbal, String quantitative, String writing) {
super();
this.verbal = verbal;
this.quantitative = quantitative;
if(writing!=null && !writing.isEmpty())
this.writing = writing;
else
pre2005=true;
if(pre2005)
name="SAT (pre 2005)";
else
name="SAT";
}
public String getVerbal() {
return verbal;
}
public void setVerbal(String verbal) {
this.verbal = verbal;
}
public String getQuantitative() {
return quantitative;
}
public void setQuantitative(String quantitative) {
this.quantitative = quantitative;
}
public String getWriting() {
if(!this.pre2005)
return writing;
else
return "";
}
public void setWriting(String writing) {
this.writing = writing;
}
public boolean isPre2005() {
return pre2005;
}
public void setPre2005(boolean pre2005) {
this.pre2005 = pre2005;
}
}
}
It keeps saying:
No enclosing instance of type AddBoardTestCommand.BoardTestBean is
accessible. Must qualify the allocation with an enclosing instance of
type AddBoardTestCommand.BoardTestBean (e.g. x.new A() where x is an
instance of AddBoardTestCommand.BoardTestBean).
when I try to do this:
SAT bean = new SAT();
with SAT imported as AddBoardTestCommand.BoardTestBean.SAT
I don’t understand why it is asking me to initialize the BoardTestBean class when it is abstract. It is meant to just hold the values for several subclasses (SAT is not the only subclass. I just omitted the others for simplicity).
Can anyone tell me what’s wrong? Thanks.
This is because
SATclass is an inner class ofBoardTestBean, but not a static inner class. Only static inner classes can be instantiated without an “enclosing” instance context; non-static need a “parent” instance.If
SATdoes not need to use any of theBoardTestBean‘s state, declare itstatic; otherwise, add an instance method toBoardTestBeanand instantiateSATfrom there.P.S. I am assuming that you are accessing
SATfrom the same package, because it has package visibility. If this is not intentional, you will need to make the classpublicas well.EDIT This is how you add an instance method to
BoardTestBeanreturningSAT:Now outside
BoardTestBeanyou can do this: