Initializer vs. Constructor assignment

  • Thread starter Matthias Kaeppler
  • Start date
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?
 
S

SPG

Matthias Kaeppler said:
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?


Hi,

If you compile then decompile your code, you'll find that your variables are
put into the constructor and set in there.
You should also notice that if you have multiple constructors then the same
initialisation is done in each, hence potentially bloating the class file
uneccessarily.

one way to reduce this is to always initialize your vars inside one
constructor, and in each additional constructor simply call the default
constructor first..

example:

Source code:

public class Sample {

int a = 1;
int b = 2;
int c = 3;

public Sample() {
}

public Sample(int d) {
}
}

Decompiled:

public class Sample
{

int a;
int b;
int c;

public Sample()
{
a = 1;
b = 2;
c = 3;
}

public Sample(int d)
{
a = 1;
b = 2;
c = 3;
}
}


Using default constructor....
Source:
public class Sample {

int a;
int b;
int c;

public Sample() {
a=1;
b=2;
c=3;
}

public Sample(int d) {
this();
}
}

Decompiled:

public class Sample
{

int a;
int b;
int c;

public Sample()
{
a = 1;
b = 2;
c = 3;
}

public Sample(int d)
{
this();
}
}


As you can see, if you have many constructors and many variables, you could
indeed have a rather bloated compiled class.

Hope this helps,


Steve
 
T

Tor Iver Wilhelmsen

Matthias Kaeppler said:
class A {
int a = 5;
}

is equivalent to:

class A {
int a;
A() {
a = 5;
}
}

Actually it's equivalent to this code:

class A {
int a = 0;
{
a = 5;
}
A() { }
}

because field initializations are put into every constructor, and
hence can be thought of as being in an instance initializer block.

Yes, the initialization to 0 happens first: This is done by the "new"
instruction, which occurs before the constructor is called.
 
V

Viator

Hi!
My friend is not in consent with you. Are you sure about the "new"
stuff?

Rgards,
viator
 
T

Tor Iver Wilhelmsen

Viator said:
My friend is not in consent with you. Are you sure about the "new"
stuff?

Yes, "new" initializes all fields to 0/0.0/false/null.

From

http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#19124

"[...] all the instance variables in the new object, including those
declared in superclasses, are initialized to their default values
(§2.5.1).

Just before a reference to the newly created object is returned as the
result, the indicated constructor is processed to initialize the new
object[...]"

You can see this easiy by making a class with two variables, where you
only set one of them to a non-default value: Running javap -c on the
resulting class will show that it only creates value assignment to the
one you assigned. If your friend was right, it would also need to add
code to set the other to 0/0.0/false/null.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top