enforce a method to be called java -
i have set of classes a1 an of such form :
public class a1{ //constructor method public a1(...){...} //init method public void init(){...} //some class specific method ... } i ensure method init called after class initialization (possibly compiler error or sort of warning if not used) .
something like:
a1 a1=new a1(...); //some other code may relate a1 (e.g. changing default global values) a1.init(); // <-- if not used bad things happens. the limitation is:
that cannot use init() method within constructor
so there way of doing it? (except calling init() in constructor of course).
create boolean in class false, set true in init() , avoid call method if not true.
public class a1{ boolean initialized = false; //constructor method public a1(...){...} //init method public void init() { // init stuff initialized = true; } //some class specific method public void mymethod() { if(!initialized) { // print error } else { // stuff } } ... }
Comments
Post a Comment