What is the difference between initializing a class property inline vs constructor

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












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?









share|improve this question



























    up vote
    4
    down vote

    favorite












    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?









    share|improve this question























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      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?









      share|improve this question













      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?






      apex






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      Eduard

      1,622521




      1,622521




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          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.







          share|improve this answer






















          • Thanks @Jayant!
            – Eduard
            1 hour ago






          • 1




            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
            1 hour ago










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "459"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f232515%2fwhat-is-the-difference-between-initializing-a-class-property-inline-vs-construct%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          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.







          share|improve this answer






















          • Thanks @Jayant!
            – Eduard
            1 hour ago






          • 1




            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
            1 hour ago














          up vote
          3
          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.







          share|improve this answer






















          • Thanks @Jayant!
            – Eduard
            1 hour ago






          • 1




            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
            1 hour ago












          up vote
          3
          down vote



          accepted







          up vote
          3
          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.







          share|improve this answer














          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.








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 1 hour ago









          Jayant Das

          6,1821319




          6,1821319











          • Thanks @Jayant!
            – Eduard
            1 hour ago






          • 1




            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
            1 hour ago
















          • Thanks @Jayant!
            – Eduard
            1 hour ago






          • 1




            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
            1 hour ago















          Thanks @Jayant!
          – Eduard
          1 hour ago




          Thanks @Jayant!
          – Eduard
          1 hour ago




          1




          1




          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
          1 hour ago




          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
          1 hour ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f232515%2fwhat-is-the-difference-between-initializing-a-class-property-inline-vs-construct%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery