N0W0RK N0W0RK - 2 months ago 6
Java Question

Java No enclosing instance of type is accessible

there I'm pretty new to Java and have german class and method titles. This Code is meant to give a string output for every class extending "Musiker". I have already looked on SO but my problem is that changing it to static gives an error on the class itself. The main reason why I open a new Question is, that every other class is working as planned. And please don't wonder why the Strings look weird, the Book I copied this from is meant to be humoristic.

public class Proberaum {
public static void main(String[] args) {

try {

Musiker saenger = new Saenger();
Musiker gitarrist = new Gitarrist();
Musiker bassist = new Bassist();
Musiker trompeter = new Trompeter();
Musiker backgroundSaengerin = new BackgroundSaengerin();
machtMusik(saenger, gitarrist, bassist, trompeter, backgroundSaengerin);

} catch(Exception e) {
new Exception().printStackTrace();
}
}

public static void machtMusik(Musiker... gruppe) {
for(Musiker musiker : gruppe) {
musiker.musizieren();
}
}


public class Musiker {

private String name;
private int alter;
private Band band;

public void musizieren() {
System.out.println("OO Mmmmmmmmh, OO Mmmmmmmmh");
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAlter() {
return alter;
}

public void setAlter(int alter) {
this.alter = alter;
}

public Band getBand() {
return band;
}

public void setBand(Band band) {
this.band = band;
}
}

public class Band {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
public class Saenger extends Musiker {

@Override
public void musizieren() {
this.singen();
}

public void singen() {
System.out.println("Oh, bäbi, juuuu a mei sannnnscheiiiiin");
}
}
public class BackgroundSaengerin extends Saenger {
}

public class Bassist extends Musiker {
}

public class Gitarrist extends Musiker {

public void musizieren() {
System.out.println("Tschiiiiiingzäääängggggg");
}
}
public class Trompeter extends Musiker {

}


}

Answer

Like Joe C also mentioned in his answer: the core of the problem is that your classes Saenger, Musiker, etc etc. are all nested classes (nested inside Proberaum), but they are defined as non-static.

In Java, non-static nested classes are called "inner classes". Inner classes have implicit access to their enclosing class members (even private ones), but of course the flipside of this is that there first needs to be an object of that enclosing class for the inner class to reference. That is why the compiler is complaining in your example: you're trying to create an object of class Saenger, which is an inner class of Proberaum, so to create that object it needs to have a reference to an object of type Proberaum. Since you're doing the object creation in the (static) main method, no such object exists.

So, to fix, you have to change your inner classes. Easiest is to declare them all static. Note that you can do this is in addition to be making them public:

 public static class Seanger extends Musiker { ... 

As also remarked elsewhere however, you really should not put every class in the same file. Learn to work with one class per file, it's the Java Way™.

Comments