1

What is the difference, if any, in creating a member inside a constructor, or outside of it?

Example 1:

public class Person
{
List<Person> friends = new List<Person>();

public Person()
{

}

}

Example 2:

public class Person
{
List<Person> friends;

public Person()
{
friends = new List<Person>();
}

}

I've used C# as an example, but this applies to any oop language.

Is there a practical difference between the two? I've had Example 2 being described to me as a cleaner way of implementing it for some reason.

Haedrian
  • 4,240
  • 2
  • 32
  • 53
  • possible duplicate: http://stackoverflow.com/questions/4916735/default-constructor-vs-inline-field-initialization?rq=1 – Hari Menon Oct 19 '13 at 19:41

3 Answers3

1

In C# I dont think it matters a lot. At the time when instance is constructed then variables(if any) which are initialized at declaration will be initialized before the constructor is run. And if these variables are not acccessed then there is no functional difference between them

For better reference check this:- Best Practice: Initialize class fields in constructor or at declaration?

Quoting one line which is really important from the above link(said by Quibblesome)

In terms of best practice the former is less error prone than the latter as someone could easily add another constructor and forget to chain it.

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Plagiarizing the Oracle Java Tutorials here: http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

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

public class BedAndBreakfast {
    private boolean full = false;
}

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.

sdanzig
  • 4,510
  • 1
  • 23
  • 27
1

I will try to analyse it from Java's perspective. Unless your variable/member is a static one it doesn't matter. Example 2 is sometimes referred to as lazy initialization where you are deferring initialization of friends list till the constructor is called. But as friends is not static compiler treats your Example 1 in similar fashion as Example 2. From HERE

Field declarations, however, are not part of any method, so they cannot be executed as statements are. Instead, the Java compiler generates instance-field initialization code automatically and puts it in the constructor or constructors for the class. The initialization code is inserted into a constructor in the order it appears in the source code, which means that a field initializer can use the initial values of fields declared before it.

So compiler just converts your Example 1 code to Example 2 so that it can be initialized in the similar fashion.

Community
  • 1
  • 1
Prateek
  • 1,916
  • 1
  • 12
  • 22