Answers

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.

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