7

There is a similar question What is the difference between initializing properties in shorthand vs constructor? on this forum, but I wanted to get more descriptive explanation on the "difference" (pros/cons and obvious use cases) between:

Property is initialized inline

public with sharing class Bar {
    private Foo fooProperty = new Foo();

public Bar() {}

}

Property is initialized in constructor

public with sharing class Bar
{
    private Foo fooProperty;

    public Bar()
    {
        fooProperty = new Foo();
    }
}

I hope this this question is not too broad, taking into consideration the questions below:

  1. What are the main differences between these approaches and when should I prefer one over the other? I see one difference - place where = new Foo(); is placed in the code.
  2. Given a scenario when the new Foo() statement throws an exception, how should the client that instantiate Bar (for instance, Bar b = new Bar();) properly handle it?

3 Answers 3

5

Your #2 actually addresses most part of it.

You should initialize a property during its declaration only if you know that it does not throw exceptions. Having it in constructor will let you address it.

From a difference perspective, there's none. Sometimes choosing one over the other provides more readability.

This is a very good explanation available on Java documentation:

As you have seen, you can often provide an initial value for a field in its declaration.

This works well when the initialization value is available and the initialization can be put on one line. However, this form of initialization has limitations because of its simplicity. If initialization requires some logic (for example, error handling or a for loop to fill a complex array), simple assignment is inadequate. Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks.

0
1

As a general rule of thumb, static variables can be initialized via public static foo = bar, while in almost all other situations, you either have a constructor for doing other stuff, you need to handle exceptions, and/or you need getters and setters and can't assign a value directly anyways. As another consideration, your code will be easier to read if you do all your initialization in one area; either initialize all your variables using the inline assignment, or use a constructor. Avoid mixing the two, because that can make it more challenging to read your code.

1

Apart from the other things mentioned here, one most important thing is the order of execution. So if you have initializations in both places, you should know what will be the eventual state. The rules are:

  1. Field initializers run first
  2. Constructor code runs next

Also note that if this is an inherited class, you base constructor + its field initializers will run before everything else.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .