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)"
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.
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.
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.
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.
Your classes are currently inner classes, which need to be constructed in the context of instances of the containing class. Your options are:
static
classes, at which point they won't be inner classes any morePersonally 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?
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);
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With