samedi 16 juillet 2016

Access java class variable from parent and then to child

I have four classes below, 1st is independent, 2nd is parent, and 3rd & 4th are child:

public class GreenhouseControls {
public boolean light = false;
 }

public class Event {
GreenhouseControls ghc = new GreenhouseControls();
}

public class extends LightOn {
ghc.light = true; //creates a new instance of ghc.
}


public class extends LightOff {
ghc.light = false; //creates a new instance of ghc again.
}

I want to avoid creating new instances. I am doing object serialization, so the light get a new instance everytime and its not getting saved when I write the GreenhouseControls in a file. Secondly, I know static variable is an option, but it is limiting my task. Is there any ideas you guys can give me ? I am badly stuck here. Thanks.

Aucun commentaire:

Enregistrer un commentaire