question relates to instance variable initialization

S

Shawn

Hi,

I am unclear about the following two ways to initialize instance variables.

public class MyClass
{
private int num = 9;

...

}

public class MyClass
{
private int num;

public MyClass()
{
this.num = 9;
}
}

What is the difference between the two? What is the consequence?

Thank you very much.
 
S

Steve W. Jackson

Shawn <[email protected]> said:
Hi,

I am unclear about the following two ways to initialize instance variables.

public class MyClass
{
private int num = 9;

...

}

public class MyClass
{
private int num;

public MyClass()
{
this.num = 9;
}
}

What is the difference between the two? What is the consequence?

Thank you very much.

<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4
..12.5> says that primitive variables of type int get a default value of
0. So in the latter example, an automatic initialization occurs when
the class is loaded, and then the value is set in the no-arg constructor
you show. No real consequence of note, just a different place to
initialize the value.

= Steve =
 
M

Mark Jeffcoat

Shawn said:
Hi,

I am unclear about the following two ways to initialize instance variables.

public class MyClass
{
private int num = 9;

...

}

public class MyClass
{
private int num;

public MyClass()
{
this.num = 9;
}
}

What is the difference between the two? What is the consequence?

Thank you very much.


Java defines a clear order of initialization; in your example,
the constructor is executed after num is implicitly set to
zero.

The Java language spec defines this, of couse, but I'm really
fond of Bruce Eckel's write-up in his excellent _Thinking in Java_.
He's made some editions of that book available online;

http://www.faqs.org/docs/think_java/TIJ306.htm#Heading3959

directly addresses your question, and also talks about
where statics fit in.
 
M

Mike Schilling

Shawn said:
Hi,

I am unclear about the following two ways to initialize instance
variables.

public class MyClass
{
private int num = 9;

...

}

public class MyClass
{
private int num;

public MyClass()
{
this.num = 9;
}
}

What is the difference between the two? What is the consequence?

Iintializations of instance variables are effectively added to every
constructor, placed directly after the call to the superclass's constructor.
Asuming that the only constructor for MyClass() is the one shown, your two
examples behave exactly alike. To be a bit more explicit,

public class MyClass
{
private int num = 9;

public class MyClass()
{
System.out.println("made a MyClass");
}
}

is exactly the same as

public class MyClass
{
private int num;

public class MyClass()
{
super();
num = 9;
System.out.println("made a MyClass");
}
}
 
T

trippy

Shawn took the hamburger said:
Hi,

I am unclear about the following two ways to initialize instance variables.

public class MyClass
{
private int num = 9;

...

}

public class MyClass
{
private int num;

public MyClass()
{
this.num = 9;
}
}

public class GlobalVSConst {

private int aNum = 9;


GlobalVSConst() {

this.aNum = 2;

}

public int getANum() {

return aNum;

}


public static void main(String[] args) {

GlobalVSConst gvc = new GlobalVSConst();
System.out.println(gvc.getANum());

}

}
What is the difference between the two? What is the consequence?

Thank you very much.

--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Aces High" -- Iron Maiden

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"
 
C

Chris Uppal

Shawn said:
I am unclear about the following two ways to initialize instance
variables. [...]
What is the difference between the two? What is the consequence?

There isn't a lot of difference. It's just a question of how you want to
structure your code. If you have several independent constructors (i.e. they
don't all delegate down to one basic constructor), or if you have no explicit
constuctors at all; then the initialiser syntax is useful. If not, then you
might as well initialise everything in the basic constructor.

Personally, I like to use initialiser for anything which is set independently
of data passed to any constructor -- just to make it clear that it /is/
independent.

-- chris
 
S

Simon Brooke

Shawn ('[email protected]') said:
Hi,

I am unclear about the following two ways to initialize instance
variables.

public class MyClass
{
private int num = 9;

...

}

public class MyClass
{
private int num;

public MyClass()
{
this.num = 9;
}
}

What is the difference between the two? What is the consequence?

The second is more bug prone. I don't like leaving uninitialised variables
about - at some stage you're going to take the 'this.num = 9' line out
without taking the 'private int num' line out, and then you've got a
runtime error which can be obnoxiously difficult to debug.
 
L

Lew

Shawn said:

Simon said:
The second is more bug prone. I don't like leaving uninitialised variables
about - at some stage you're going to take the 'this.num = 9' line out
without taking the 'private int num' line out, and then you've got a
runtime error which can be obnoxiously difficult to debug.

Initializing num to zero instead of nine will not necessarily crash the
program, depending on other logic.

Setting it to the wrong value is pretty likely to yield incorrect results.
I'm not sure that would be "obnoxiously difficult to debug". Any debugger
lets you examine the value of variables and set breakpoints, so one could
simply set a breakpoint at the point where the error is detected and examine
the value of num to see if it is what's expected.

Of course, debugging generally is difficult. Whether such difficulty is
"obnoxious" is a function of one's tolerance for the difficulty and how much
more difficult one bug is to trap than another. I assess the difficulty of
detecting an incorrect value in a variable as typically obnoxious, not more so
than most debugging tasks. It is probably the most common task of all in
debugging.

- Lew
 
C

Chris Uppal

Simon said:
The second is more bug prone. I don't like leaving uninitialised variables
about - at some stage you're going to take the 'this.num = 9' line out
without taking the 'private int num' line out, and then you've got a
runtime error which can be obnoxiously difficult to debug.

In what sense is:

private int num = 9;

more reliable than:

private int num = 0;

? I don't see a difference. Not unless 9 had been specially choosen to be an
instantly recognisable "illegal" value, to help debugging. But in that case, I
don't think 9 is very well choosen -- I'd expect to see something
like -111111111.

But:

private int num = 0;

is identical to:

private int num;

for all practical, and most theoretical, purposes. So, (unless you are using
the illegal-value technique) it follows that explicit initialisation doesn't
increase reliabilty.

Or, to be more accurate, it doesn't increase reliability for that reason -- if
it avoids code duplication, assigning num = 9 in two or more constructors, then
that is definitely a /big/ plus.

-- chris
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top