The first method gives you custom control over how to initialize the class object that you're dealing with per constructor, whereas the second option will initialize the Family object the same way for all constructors.
The first method is preferable, because it allows you to take a Family object into your constructor allowing you to do things like dependency injection, which is generally good programming practice (and allows for easier testing).
Aside from that, both of them are member variables (not static as someone else said, you have to use the static keyword for that). Each instance of this class will contain an instance of a Family object this way.
I would recommend something like this
public class Person {
public Person(Family family) {
this.family = family;
}
Family family;
}
Edit:
To address the comment that was made about my post (which was accurate, somewhat), there is a difference in which the order of the objects are initialized. However, the comment stated that the order of operation is:
initialization of instances variables -> instance initializer -> constructor
From testing this out, it seems that it depends on which occur first in the code, initialization of instance variables or instance initializers, and then the constructor is called after both of them. Consider the example where the constructor of the Family object just prints out the string given to it:
public class Person {
{
Family familyB = new Family("b");
}
Family familyA = new Family("a");
Family familyC;
public Person() {
this.familyC = new Family("c");
}
}
results in this output when constructing a person object:
Family: b
Family: a
Family: c
but this code:
public class Person {
Family familyA = new Family("a");
{
Family familyB = new Family("b");
}
Family familyC;
public Person() {
this.familyC = new Family("c");
}
}
results in this output when constructing a person object:
Family: a
Family: b
Family: c