What is the difference between initializing a class property inline vs constructor
Clash 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:
- 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. - 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
add a comment |Â
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:
- 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. - 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
add a comment |Â
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:
- 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. - 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
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:
- 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. - 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
apex
asked 1 hour ago
Eduard
1,622521
1,622521
add a comment |Â
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password