java - Problems with initializing fields to null for widgets -


i going through codes views , noticed following lot:

public class foopanel extends jpanel {    private jcheckbox checkbox = null;    private jcombobox combobox = null;      ...     protected void initview() {        checkbox = new jcheckbox();        combobox = new jcombobox();    } } 

are there problems associated initializing fields null though null w/o declaring it?

one thing saw 2 instances of object created if initialize null follows:

public class foopanel extends jpanel {    private jpanel innerpanel = null;     ...     protected void initview() {        add.(getinnerpanel());    }     private jpanel getinnerpanel() {        if(innerpanel == null) {            innerpanel = new jpanel();        }         return innerpanel;    } } 

in case, 2 innerpanels. also, convention? should write getwidget() each widget in panel? think generated windowsbuilder plugin in eclipse.

thanks!!

regarding assigning member values null:

there aren't going problems associated assigning value null member during declaration (or @ times other declaration). 1 advantage type of assignment makes code explicit , makes intention clear reading code later. when member variable not assigned value during declaration, isn't clear whether author intended value null or forgot write assignment code.

regarding 2 innerpanel instances

the initial assignment null, followed assigning value @ time of need (as done in code), won't cause 2 instances created. it's not clear code shown how getinnerpanel method being used, may be running multi-threaded scenario 2 threads may calling getinnerpanel @ same time.

in scenario, end multiple innerpanel member assignments (and duplicate construction). possible first thread check value of innerpanel, find null (thereby entering associated code within if condition), , run jpanel construction , assignment code. if different thread runs same code, @ same time, second thread may perform same innerpanel value check, find value still null (because code within if condition has not yet performed construction , assignment), , enter same construction , assignment code, resulting in multiple construction occurrences.

you make innerpanel access synchronized, within getinnerpanel method, prevent happening:

first synchronization option:

private synchronized jpanel getinnerpanel() {     if(innerpanel == null) {         innerpanel = new jpanel();     }     return innerpanel; } 

second synchronization option:

private jpanel getinnerpanel() {     synchronized(innerpanel) {         if(innerpanel == null) {             innerpanel = new jpanel();         }     }     return innerpanel; } 

the second option better option, because reduces scope of code synchronized, not much. , if expect getinnerpanel method called many times, neither of options shown may best option, because every thread calls method slowed down synchronization , null check.

in example, key trade-off centers around innerpanel member. if know innerpanel created or created, may better remove lazy initialization (where jpanel constructed @ time-of-need), construct jpanel part of innerpanel declaration , assignment, remove need null check within getinnerpanel method. up-front jpanel construction removes need null check , getinnerpanel method return reference innerpanel member.

if, on other hand, have panels may or may not constructed, depending upon need, lazy initialization have implemented makes perfect sense.

regarding method each jpanel

having method each panel makes sense, if feel makes code clearer , easier use, , if pattern plan implement across project. said, associated lazy initialization , associated synchronized blocks of code should examined (based on trade-off considerations described above) , implemented in way best matches way believe panels needed , constructed.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -