Getting the error "No instance of type ..."

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;
        }
    }
}

      

He goes on to say:

There is no closing instance of type AddBoardTestCommand.BoardTestBean. available. To qualify the distribution using the supplied instance, enter AddBoardTestCommandBoardTestBean (for example, x.new A (), where x is an instance of AddBoardTestCommand.BoardTestBean).

when i try to do this: SAT bean = new SAT (); with SAT imported asAddBoardTestCommand.BoardTestBean.SAT

I don't understand why it is asking me to initialize the class BoardTestBean

when it is abstract. It is designed to just hold values ​​for multiple subclasses (SAT is not the only subclass. I just omitted the others for simplicity).

Can anyone tell me what happened? Thank you.

+3


source to share


2 answers


This is because the class SAT

is an inner class BoardTestBean

, but not a static inner class. Only static inner classes can be instantiated without the "enclosing" instance context; non-static needs a "parent" instance.

If you SAT

don't need to use any of the states BoardTestBean

, declare it static

; otherwise, add an instance method to BoardTestBean

and instantiate SAT

from there.

PS I'm assuming you are accessing SAT

from the same package because it has package visibility. If this is not intentional, you will need to create a class as well public

.

EDIT . How do you add an instance method to BoardTestBean

, returning SAT

:



SAT makeSAT() {
    return new SAT();
}

      

Now outside BoardTestBean

you can do this:

// assuming that you have an instance of BoardTestBean...
BoardTestBean myBean = ...
BoardTestBean.SAT = myBean.makeSAT();

      

+5


source


You defined an inner class that hides this for your outer class.

Just do



public static class SAT {

      

and you get a nested class without this hidden

+2


source







All Articles