Baikol February 2016

Problems with initializing fields to null for widgets

I was going through some codes for views and noticed the following a 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 any problems associated with initializing fields to null even though they are null w/o you declaring it?

One thing I saw was that 2 instances of an object are created if you initialize to null as 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 this case, I get 2 innerPanels. Also, what is the convention? Should you write a getWidget() for each widget in a panel? I think those were generated by WindowsBuilder plugin in Eclipse.

Thanks!!

Answers


HTNW February 2016

There is no difference whether you assign a field to null or leave it untouched. The two cases compile to exactly the same thing (see bottom of answer for bytecode).

As for 2 instances being created: I don't see how two would be created. If innerPanel hasn't been set and is null, a JPanel is constructed and stored. If it does exist, it is returned without touching anything. Whatever you do with that is your choice.

Bytecode:

$ cat TestNull.java
public class TestNull {
    public Object obj = null;
}
$ cat TestBlank.java
public class TestBlank {
    public Object obj;
}
$ javap -c TestNull
Compiled from "TestNull.java"
public class TestNull {
    public java.lang.Object obj;

    public TestNull();
      Code:
       0: aload_0
       1: invokespecial #1                  // super()
       4: aload_0
       5: aconst_null
       6: putfield      #2                  // obj = null
       9: return
}
$ javap -c TestBlank
Compiled from "TestBlank.java"
public class TestBlank {
    public java.lang.Object obj;

    public TestBlank();
      Code:
       0: aload_0
       1: invokespecial #1                  // super()
       4: aload_0
       5: aconst_null
       6: putfield      #2                  // obj = null
       9: return
}


Sean Mickey February 2016

Regarding assigning member values to null:

There aren't going to be any problems associated with assigning the value null to a member during declaration (or at times other than declaration). One advantage to this type of assignment is that it makes the code explicit and makes your intention very clear to anyone reading the code later. When a member variable is not assigned a value during declaration, it isn't clear whether the author intended the value to be null or simply forgot to write the assignment code.

Regarding 2 innerPanel instances

The initial assignment to null, followed by assigning a value at time of need (as it is done in your code), won't cause 2 instances to be created. It's not clear from the code shown exactly how your getInnerPanel method is being used, but you may be running into a multi-threaded scenario where 2 threads may be calling getInnerPanel at about the same time.

In this scenario, you could end up with multiple innerPanel member assignments (and duplicate construction). It is possible for the first thread to check the value of innerPanel, find that it is currently null (thereby entering the associated code within the if condition), and then run the JPanel construction and assignment code. If a different thread runs the same code, at about the same time, the second thread may perform the same innerPanel value check, find that the value is still null (because the code within the if condition has not yet performed the construction and assignment), and enter the same construction and assignment code, resulting in multiple construction occurrences.

You could make the innerPanel access synchronized, within the getInnerPanel method, to prevent this from happe

Post Status

Asked in February 2016
Viewed 2,508 times
Voted 12
Answered 2 times

Search




Leave an answer