I'm running into a problem that I can't solve basically, I'm trying to build a code for a house and I wanna make multiple calls to the house without having to turn on the light every single time
My field value
private boolean turnon = false;
and then my method to turn on the the lights
public void turnonn(){
turnon = true;
}
Basically what I want to avoid is to not call the method every single time I wanna add a new House, basically once I turn it on it turns on for every instance of the class.
House x = new House();
x.turnon;
I tried defining the method statically but it didnt work, any suggestions would be appreicaated
CodePudding user response:
In your House class, add this:
public House() {
turnon();
}
CodePudding user response:
Whenever you are creating a new house, you can initialize the value of field turnOn(notice the camelcase convention ) in the constructor.
public House() {
// initializing other fields
turnOn = true;
}
Then you can provide regular getter and setter to access the turnOn field.
