Initialize objects on initialization?

I have a class that contains other objects (List, Set and objects from my application) in it.

public class SomeClass {
    private List l;
    private SomeObject obj;
    //...
}

      

Is it good practice to create these objects where the object is created SomeClass

to avoid NullPointerException? Something like:

public class SomeClass{
    private List l = new ArrayList();
    private SomeObject obj = new SomeObject();
    //...
}

      

Normally, these objects will be generated during processing or analysis, but errors can occur and the objects still matter null

.

+3


source to share


6 answers


Yes, this is good practice. The constructor is the natural place to instantiate member objects. You can also create them right where they are declared:

private List l = new ArrayList();

      



However, it might be a good idea to restructure or modify your code so that it NullPointerException

doesn't happen, regardless of the order in which the methods are called.

+2


source


If empty List

or the default object of this class is a valid state in every next operation, yes, create a default instance. However, if the default is not valid, then don't.



+1


source


Well, I prefer to initialize because there is sometimes a headache to find a null pointer exception, and if u initializes your objects with constructors, the object inside must be initialized.

Hope it helps you.

+1


source


It is generally a good idea to instantiate elements (whether objects or primitives) at creation time when the default (0, false, or null) is not what you want. One time to put it off, for a lazy creature. (This is used, for example, when the object might not be needed after everything is done and it is expensive to create it.) Another time to postpone this, you need to do another initialization beforehand.

Assuming you want to initialize the field at the time of object creation, there are two ways to do this: with an initializer expression, as you showed, or in the constructor (s). Not much difference other than the initializers of this instance, which are executed before the first line of the constructor. This may or may not cause problems, depending on your code logic.

It is also useful to declare member fields final

whenever they are initialized when an object is created and are not expected to change over the life of the object. An additional benefit of declaring a field final

is that the compiler will catch any initialization failure. (The compiler requires a specific assignment to properly initialize the field final

.)

+1


source


You are talking about hot construction versus lazy construction. There are places where everyone matters.

In many situations it is better to lazily create things as it saves memory. But then you have to check for null every time you try to get data.

In some situations, it makes sense to create an object up front to avoid the above mentioned null check or to avoid object creation time during an intensive process.

+1


source


It's ok to generate them like this, but it's not a good way of coding to generate them to avoid NPEs. There should be correct code validation, not assignment of garbage-eligible objects that will not be used.


You can also assign some default states - for example Collections.emptyList () or in a constant class:

DEFAULT_STATE = new SomeState();

      

then just

class A { 
      State obj = Constants.DEFAULT_STATE;
   }

      

+1


source







All Articles