Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No enclosing instance of (class here) is accessible

Tags:

java

I have a class called GreenhouseControls that has a bunch of

classes built into it such as:

public class ThermostatNight extends Event {
    public ThermostatNight(long delayTime) {
      super(delayTime);
    }
    public void action() {
      // hardware control code here.
      thermostat = "Night";
    }
    public String toString() {
      return "Thermostat on night setting";
    }
  }

I pull values from a text file to get event names such as "ThermostatNight" and time values such as "2000". To instantiate a new object with those values I built a EventFactory that accepts the values as arguments.

This is the class I have built to create new event objects from text file values:

    public class EventFactory{

        public static Event createSpecificEvent(String eventName, long delayTime) {

            Event event = null;

            switch(eventName) {
            case "ThermostatNight": 
                event = new ThermostatNight(delayTime); // Compiler error
                break;
            case "ThermostatDay": 
                event = new ThermostatDay(delayTime);
                break;
            case "LightOn": 
                event = new LightOn(delayTime);
                break;
            case "LightOff": 
                event = new LightOff(delayTime);
                break;
                ...
        }
}

Everything was working well when I ran the program until I pulled the EventFactory class out of GreenhouseControls and into it's own independent class. Now I am getting a compile time error that says:

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

See in-line comment in EventFactory class to see where the error occurs at "new ThermostatNight(delayTime)"

like image 978
LooMeenin Avatar asked Dec 13 '13 22:12

LooMeenin


People also ask

What is enclosing class?

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.

How to instance inner class in Java?

The object of java inner class are part of the outer class object and to create an instance of the inner class, we first need to create an instance of outer class. Java inner class can be instantiated like this; OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.

Can inner class be static?

The static inner class can access the static members of the outer class directly. But, to access the instance members of the outer class you need to instantiate the outer class. Nested static class doesn't need a reference of Outer class but a nonstatic nested class or Inner class requires Outer class reference.

Can outer Java classes access inner class private member?

Conclusion. Outer classes can access inner class private members in Java until a non-static member accesses it from a static context or an inaccessible scope. Inner classes can access the variables of the outer class, including the private instance variables.


2 Answers

Your classes are currently inner classes, which need to be constructed in the context of instances of the containing class. Your options are:

  • Specify an instance of the outer class when constructing an instance of the inner class
  • Declare your nested classes as static classes, at which point they won't be inner classes any more
  • Move the nested classes out of the containing class, making them top level classes

Personally I'd go for the last option if possible - nested classes can be useful at times, but you should only use them when there's a real benefit. There are various restrictions which can be quite subtle and which are best avoided if possible. Do you have any compelling reason to make these nested classes?

like image 178
Jon Skeet Avatar answered Sep 19 '22 05:09

Jon Skeet


The error means that a non-static inner class ThermostatNight is being instantiated from a static method. You need to make the class static (the most likely solution given your code), make the method non-static, or provide an instance explicitly.

public static /*<<==Add this*/ class ThermostatNight extends Event {
    public ThermostatNight(long delayTime) {
      super(delayTime);
    }
    public void action() {
      // hardware control code here.
      thermostat = "Night";
    }
    public String toString() {
      return "Thermostat on night setting";
    }
}

To provide an instance explicitly, use this example:

public static Event createSpecificEvent(GreenhouseControls ctrl, String eventName, long delayTime) {
    Event event = null;
    switch(eventName) {
    case "ThermostatNight": 
        event = ctrl.new ThermostatNight(delayTime);
    ...
}
like image 41
Sergey Kalinichenko Avatar answered Sep 23 '22 05:09

Sergey Kalinichenko