-1

I'm learning about abstract classes in Java and I have a little problem with this error: No enclosing instance of type zad_II_1 is accessible. Must qualify the allocation with an enclosing instance of type zad_II_1 (e.g. x.new A() where x is an instance of zad_II_1). What I do wrong? This is code:

public class zad_II_1 {

    abstract class Pacjent{
            String imie;

            Pacjent(String imie){
            this.imie=imie;
            }

            abstract String nazwiskoo();
            abstract String PodajChorobe();
            abstract String PodajLek();

            String nazwisko(){
                return imie;
            }
            String choroba(){
                return PodajChorobe();
            }
            String leczenie(){
                return PodajLek();
            }
            }


     class ChoryNaGlowe extends Pacjent{
         ChoryNaGlowe(String imie){
             super(imie);}
             String nazwiskoo(){
                 return imie;
             }
             String PodajChorobe(){return "glowa";}
             String PodajLek(){return "aspiryna";}
     }
    class ChoryNaNoge extends Pacjent{
         ChoryNaNoge(String imie){
             super(imie);}
             String nazwiskoo(){
                 return imie;
             }  
             String PodajChorobe(){return "noga";}
             String PodajLek(){return "gips";}
    }
    class ChoryNaDyspepsje extends Pacjent{
         ChoryNaDyspepsje(String imie){
             super(imie);}
             String nazwiskoo(){
                 return imie;
             }   
             String PodajChorobe(){return "dyspepsja";}
             String PodajLek(){return "wegiel";}
        }





        public static void main(String[] args) {

            Pacjent[] pacjenci = { new ChoryNaGlowe("Janek"),
                                   new ChoryNaNoge("Edzio"),
                                   new ChoryNaDyspepsje("Marian")
                                 };

            for (Pacjent p : pacjenci) {
              System.out.println("Pacjent:      " + p.nazwisko() + '\n' + 
                                "Chory na:    " + p.choroba() + '\n' +
                                "Zastosowano: " + p.leczenie() +"\n\n" 
                                 );
            }

          }
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Blackchart
  • 135
  • 1
  • 3
  • 13

3 Answers3

0

All your inner class needs to be static.

static abstract class Pacjent

and

static class ChoryNaGlowe extends Pacjen

etc...

Xavier Delamotte
  • 3,519
  • 19
  • 30
0

You can make your inner classes static, of course.

Or, you need to have an instance of zad_II_1 to create the inner classes:

zad_II_1 z = new zad_II_1();
z.new ChoryNaGlowe("Janek")
Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
0

Or you can move everything out of the main method, as using the "new inner class instance" call is very confusing.

public main(String... args) {
    zad_II_1 z = new zad_II_1();
    z.doExercise();
}
private void doExercise() {
    // copied from the old main().
    // Just use new CherryNaGlowe(...), etc.
}

In general, try to use static inner classes when you can. Sometimes people store inner classes in other objects for long periods of time. If an inner class is reachable by the system, the outer class automatically is too, and that's one way to leak memory.

Eric Jablow
  • 7,874
  • 2
  • 22
  • 29