1

My code does not work. I get the following error:

No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main).

You see my code in the following:

class Main {

public class Room {
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + etage;
        result = prime * result + gebäude;
        result = prime * result + raumnummer;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Room other = (Room) obj;
        if (etage != other.etage)
            return false;
        if (gebäude != other.gebäude)
            return false;
        if (raumnummer != other.raumnummer)
            return false;
        return true;
    }

    public int gebäude;
    public int etage;
    public int raumnummer;

    public Room(int gebäude, int etage, int raumnummer) {
        super();
        this.gebäude = gebäude;
        this.etage = etage;
        this.raumnummer = raumnummer;
    }

    @Override
    public String toString() {
        String s = String.format("%2s-%s.%02d", this.gebäude, this.etage, this.raumnummer);
        return s;
    }
}

public static void main(String[] args) {
    Room office = new Room(17, 0, 10);
    Room lecture = new Room(2, 0, 10);
    Room lab = new Room(18, 1, 1);

    System.out.println(office);  // => "17-0.10"
    System.out.println(lecture); // => " 2-0.10"
    System.out.println(lab);     // => "18-1.01"
}

}

2 Answers2

2

SOLUTION 1

Separating the class Room from the class Main did the trick for me:

File Main.java:

public class Main {

public static void main(String[] args) {
    Room office = new Room(17, 0, 10);
    Room lecture = new Room(2, 0, 10);
    Room lab = new Room(18, 1, 1);

    System.out.println(office);  // => "17-0.10"
    System.out.println(lecture); // => " 2-0.10"
    System.out.println(lab);     // => "18-1.01"
}
}

File Room.java:

public class Room {
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + etage;
        result = prime * result + gebäude;
        result = prime * result + raumnummer;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Room other = (Room) obj;
        if (etage != other.etage)
            return false;
        if (gebäude != other.gebäude)
            return false;
        if (raumnummer != other.raumnummer)
            return false;
        return true;
    }

    public int gebäude;
    public int etage;
    public int raumnummer;

    public Room(int gebäude, int etage, int raumnummer) {
        super();
        this.gebäude = gebäude;
        this.etage = etage;
        this.raumnummer = raumnummer;
    }

    @Override
    public String toString() {
        String s = String.format("%2s-%s.%02d", this.gebäude, this.etage, this.raumnummer);
        return s;
    }
}

Let me investigate why this happens and I will post the reason. By now, this is a suitable fix if you don't need both classes to be on the same file.

SOLUTION 2

Make room static:

...
class Main {

public static class Room {
    @Override
    public int hashCode() {
        final int prime = 31;
...

WHY THIS HAPPENS

As stated in this post, by having Room as an inner class of Main, you are forcing the instances of Room to have an instance o Main. When using the operator new on the inner class Room without making a new instance of Main an error is produced.

By making the class Room static the class doesn't need an instance of Main.

PauMAVA
  • 1,163
  • 14
  • 23
0

This is because Room is an inner class of Main, but is not declared as a static member. When you have a non-static inner class, it can only be instantiated using an object of the outer class, otherwise the expression Main.this (which is legal in the inner class) would not have an instance of Main to refer to. Since the main method is static, there is no Main instance to pass to the Room constructor.

The solution is simply to replace the declaration public class Room with public static class Room.

kaya3
  • 47,440
  • 4
  • 68
  • 97