M
Matthias Kaeppler
Hello,
I am currently reading "Java in a Nutshell" 5th Edition (O'Reilly) and
some questions arose concerning the proper use of Field
Defaults/Initializers and Constructors.
It is stated that "The initialization code is inserted into a
constructor in the order in which it appears in the source code [...]".
Does that means that this code:
class A {
int a = 5;
}
is equivalent to:
class A {
int a;
A() {
a = 5;
}
}
or does the second code snippet first initialize a with 0 and then
assigns 5 when the ctor body is entered?
Or does all this initialization stuff boil down to be merely a matter of
style? Or are instance initializers and field defaults only thought to
be a means for initializing anonymous classes and not to be used in
other circumstances?
If not, are there any guidelines when I should initialize a class member
in the class body vs. the ctor body?
I am currently reading "Java in a Nutshell" 5th Edition (O'Reilly) and
some questions arose concerning the proper use of Field
Defaults/Initializers and Constructors.
It is stated that "The initialization code is inserted into a
constructor in the order in which it appears in the source code [...]".
Does that means that this code:
class A {
int a = 5;
}
is equivalent to:
class A {
int a;
A() {
a = 5;
}
}
or does the second code snippet first initialize a with 0 and then
assigns 5 when the ctor body is entered?
Or does all this initialization stuff boil down to be merely a matter of
style? Or are instance initializers and field defaults only thought to
be a means for initializing anonymous classes and not to be used in
other circumstances?
If not, are there any guidelines when I should initialize a class member
in the class body vs. the ctor body?