Reputation: 11
I'm quite new on Java and I need some help please.
The thing is I have a main class with a null variable object on it (required to be null at start) and a menu with different options to change this variable status.
Each option is controlled by a different class which changes the status of this variable.
How could I initialize this variable from the #1 option class and maintain the status of this variable through options without declaring this variable static?
Executing menu option #2 requires variable to be initialized.
The menu is not on the same "main" class because I'm trying to make it reusable using heritage and polymorphism for it.
Class Problem { //main class
private Control control;
}
Class Option1{
//initialize the variable control = new Control();
}
Thanks a lot!
Upvotes: 0
Views: 703
Reputation: 15624
The menu is not on the same "main" class because i'm traying to make it reusable using heritage and polimorfism for it.
Please keep in mind that inheritance is not a good way to "reuse code". Inheritance must qualify for an is a relationship. Reuse of code is better accomplished by composition.
Upvotes: 1
Reputation: 154
The best advice I could give is to make it part of the constructor in the other classes (every class except the one where you initialize it to null) and pass it every time you use the "menu"
Upvotes: 0