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?
up vote 4 down vote accepted

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.

  • Thanks @Jayant! – Eduard Sep 14 at 13:38
  • 2
    You're welcome @Eduard and that this is really a good question. We may have been using this for ages now, but sometimes revisiting this always help. – Jayant Das Sep 14 at 13:40

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.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.