Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > State of 'this' in constructor body

Reply
Thread Tools

State of 'this' in constructor body

 
 
Matthias Kaeppler
Guest
Posts: n/a
 
      04-10-2005
Hello,

I have decent knowledge of the C++ programming language, and I am
currently reading a book about Java ("Java in a Nutshell", 5th Edition,
O'Reilly).

I have a question concerning the construction of objects in Java. In
C++, the this-pointer is valid to use in the constructor body, because
C++ makes sure that when the ctor body is entered, the object has been
fully initialized to a default state, as defined by the constructor's
initializer list. This is a /good/ thing, because you are able to
provide your own initializer list, and may keep the compiler from
default-constructing a data member which value you would have hanged
anyway in the constructor (otherwise the object would have been
initialized twice, which is just brainless overhead and especially
expensive for complex user defined types).
Since Java does not have initializer lists, I can't imagine how this works:

public class Bar {

public Bar() {
this.a = 5; // which object does 'this' reference, when I
// am still in the state of initializing it?
}

}

I can only guess that the JVM takes care of default initializing each
data member before entering the constructor body, otherwise the
this-reference would reference an object which doesn't have a
well-defined state (and would probably lead to undefined behavior).

Is this true, and if this is the case, doesn't this imply a performance
hit to initialize every data member twice (in the worst case)?

Thanks in advance.

--
Matthias Kaeppler
 
Reply With Quote
 
 
 
 
Matthias Kaeppler
Guest
Posts: n/a
 
      04-10-2005
Matthias Kaeppler wrote:
> public class Bar {
>
> public Bar() {
> this.a = 5; // which object does 'this' reference, when I
> // am still in the state of initializing it?
> }


private int a;

> }


Sorry.

--
Matthias Kaeppler
 
Reply With Quote
 
 
 
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      04-10-2005
Matthias Kaeppler <> writes:

> public class Bar {
>
> public Bar() {
> this.a = 5; // which object does 'this' reference, when I
> // am still in the state of initializing it?
> }


No, the object is constructed: You are now initializing it. The
constructor is synthesized into a specially treated void instance
method.

> I can only guess that the JVM takes care of default initializing each
> data member before entering the constructor body,


It does.

> Is this true, and if this is the case, doesn't this imply a
> performance hit to initialize every data member twice (in the worst
> case)?


No, because the default initialization to 0/0.0/null is done very
rapidly by the "new" keyword execution.
 
Reply With Quote
 
Matthias Kaeppler
Guest
Posts: n/a
 
      04-10-2005
Tor Iver Wilhelmsen wrote:
> Matthias Kaeppler <> writes:
>
>
>>public class Bar {
>>
>> public Bar() {
>> this.a = 5; // which object does 'this' reference, when I
>> // am still in the state of initializing it?
>> }

>
>
> No, the object is constructed: You are now initializing it. The
> constructor is synthesized into a specially treated void instance
> method.
>
>
>>I can only guess that the JVM takes care of default initializing each
>>data member before entering the constructor body,

>
>
> It does.
>
>
>>Is this true, and if this is the case, doesn't this imply a
>>performance hit to initialize every data member twice (in the worst
>>case)?

>
>
> No, because the default initialization to 0/0.0/null is done very
> rapidly by the "new" keyword execution.


Okay, thanks Tor.

--
Matthias Kaeppler
 
Reply With Quote
 
Matthias Kaeppler
Guest
Posts: n/a
 
      04-10-2005
Tor Iver Wilhelmsen wrote:
> Matthias Kaeppler <> writes:
>
>
>>public class Bar {
>>
>> public Bar() {
>> this.a = 5; // which object does 'this' reference, when I
>> // am still in the state of initializing it?
>> }

>
>
> No, the object is constructed: You are now initializing it. The
> constructor is synthesized into a specially treated void instance
> method.
>
>
>>I can only guess that the JVM takes care of default initializing each
>>data member before entering the constructor body,

>
>
> It does.
>
>
>>Is this true, and if this is the case, doesn't this imply a
>>performance hit to initialize every data member twice (in the worst
>>case)?

>
>
> No, because the default initialization to 0/0.0/null is done very
> rapidly by the "new" keyword execution.


On a second thought, this raises the question:

Does it make sense at all to explicitly initialize data members with
0/0.0/null? I suppose if you do, it's optimized away by the runtime
anyway (since you said, the runtime does this implicitly anyway before
entering the ctor body).

e.g.:

public class Bar {

public Bar() {
foo_ = null; // this is superfluous, right?
// (given that foo_ is considered to be
// constructed later in program execution)
}

Foo foo_;

}

I once heard that it'd be considered good style to do that, but after
what you've said, it's just right out useless, ain't it?

--
Matthias Kaeppler
 
Reply With Quote
 
Kevin McMurtrie
Guest
Posts: n/a
 
      04-10-2005
In article <d3bjis$dg1$04$>,
Matthias Kaeppler <> wrote:

> Tor Iver Wilhelmsen wrote:
> > Matthias Kaeppler <> writes:
> >
> >
> >>public class Bar {
> >>
> >> public Bar() {
> >> this.a = 5; // which object does 'this' reference, when I
> >> // am still in the state of initializing it?
> >> }

> >
> >
> > No, the object is constructed: You are now initializing it. The
> > constructor is synthesized into a specially treated void instance
> > method.
> >
> >
> >>I can only guess that the JVM takes care of default initializing each
> >>data member before entering the constructor body,

> >
> >
> > It does.
> >
> >
> >>Is this true, and if this is the case, doesn't this imply a
> >>performance hit to initialize every data member twice (in the worst
> >>case)?

> >
> >
> > No, because the default initialization to 0/0.0/null is done very
> > rapidly by the "new" keyword execution.

>
> On a second thought, this raises the question:
>
> Does it make sense at all to explicitly initialize data members with
> 0/0.0/null? I suppose if you do, it's optimized away by the runtime
> anyway (since you said, the runtime does this implicitly anyway before
> entering the ctor body).
>
> e.g.:
>
> public class Bar {
>
> public Bar() {
> foo_ = null; // this is superfluous, right?
> // (given that foo_ is considered to be
> // constructed later in program execution)
> }
>
> Foo foo_;
>
> }
>
> I once heard that it'd be considered good style to do that, but after
> what you've said, it's just right out useless, ain't it?


You can also declare field values along with the type.

Foo m_foo= null;

This gets run by the <init> routine right before the constructor is
executed. They should be fairly simple values because <init> is tricky
to debug.
 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      04-10-2005
Matthias Kaeppler wrote:
> Hello,
>
> I have decent knowledge of the C++ programming language, and I am
> currently reading a book about Java ("Java in a Nutshell", 5th Edition,
> O'Reilly).
>
> I have a question concerning the construction of objects in Java. In
> C++, the this-pointer is valid to use in the constructor body, because
> C++ makes sure that when the ctor body is entered, the object has been
> fully initialized to a default state, as defined by the constructor's
> initializer list. This is a /good/ thing, because you are able to
> provide your own initializer list, and may keep the compiler from
> default-constructing a data member which value you would have hanged
> anyway in the constructor (otherwise the object would have been
> initialized twice, which is just brainless overhead and especially
> expensive for complex user defined types).
> Since Java does not have initializer lists, I can't imagine how this works:
>
> public class Bar {
>
> public Bar() {
> this.a = 5; // which object does 'this' reference, when I
> // am still in the state of initializing it?
> }
>
> }
>


The reason for initializer lists in C++ is due to c++ refs always having
to be initialised to refer to some object. However, Java does not have
these types of references - Javas can either point to nothing (i.e.
NULL) or to any object of the same or derived type of the reference.

Therefore, because Java refs can legally point to null, we don't need
initializer lists. Also, because Java refs can point to null, member
var references that are not explicitly initialised are automatically
initialised to point to NULL, so no default constructors are called.

e.g.

class Foo {
private String aString; // auto assigned to NULL;
private String string2 = null; // legal but not needed.
}

> I can only guess that the JVM takes care of default initializing each
> data member before entering the constructor body, otherwise the
> this-reference would reference an object which doesn't have a
> well-defined state (and would probably lead to undefined behavior).
>
> Is this true, and if this is the case, doesn't this imply a performance
> hit to initialize every data member twice (in the worst case)?
>
> Thanks in advance.
>


If you have a debugger in your IDE, it would be worth putting a
breakpoint on the constructor and stepping into it, in order to help you
understand how objects are created in Java.

To get the best out of this simple technique - you'd want to use a
mixture of primitives and objects members, some of which are initialised
and some that aren't.

e.g.



public class Bar {

public Bar() { // Put break point here!

// nice little test here...lets use one member to init another..
this.a = secondIntObj.intValue();

// what value will firstIntObj ref to here?
firstIntObj = new Integer(a);
}

private int a;
private int a2 = 1;
private Integer firstIntObj;
private Integer secondIntObj = new Integer(2);
}


 
Reply With Quote
 
Johan Poppe
Guest
Posts: n/a
 
      04-10-2005
Matthias Kaeppler wrote:

>Does it make sense at all to explicitly initialize data members with
>0/0.0/null? I suppose if you do, it's optimized away by the runtime
>anyway (since you said, the runtime does this implicitly anyway before
>entering the ctor body).
>
>e.g.:
>
>public class Bar {
>
> public Bar() {
> foo_ = null; // this is superfluous, right?
> // (given that foo_ is considered to be
> // constructed later in program execution)
> }
>
> Foo foo_;
>
>}
>
>I once heard that it'd be considered good style to do that, but after
>what you've said, it's just right out useless, ain't it?


Most of the things that are "considered good style" have nothing to do
with how the program runs, but with how the code is read by humans. As
with this thing. It's superfluous and useless in terms of impact on
the result of the program, but it might be considered good style in
terms of the programmer declaring to anyone reading the code later
that "The foo_ variable is not properly initialized in the
constructor, it's still null when the constructor has finished, and
I'm aware of that and it's intended and bla bla bla."

I do this myself sometimes, but far from always. Which I guess is
really bad style.

--
Riktig sitering gjør meldingene dine lettere å lese:
< url: http://home.online.no/~vidaandr/news/OBSquoting.html >
 
Reply With Quote
 
=?iso-8859-1?q?Markus_B._Kr=FCger?=
Guest
Posts: n/a
 
      04-11-2005
Andrew McDonagh <> writes:

> The reason for initializer lists in C++ is due to c++ refs always
> having to be initialised to refer to some object.


Another reason is that C++ objects may have member variables that
aren't refs, but are objects of a class with no default constructor
(that is to say, no public constructor with no arguments). The
enclosing object's constructor therefore needs some way to provide
argument values for the members' constructors, since the members are
initialized before the main body of the enclosing object's constructor
is entered. As you wrote, Java does not need this, since all member
variables in Java are references initialized to null.

--
,------------------- Markus Bjartveit Krüger ---------------------.
' `
` E-mail: WWW: http://www.pvv.org/~markusk/ '
)-------------------------------------------------------------------(
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
To reduce your body weight & slim your body Loss weight MCSA 0 07-23-2007 07:54 PM
To reduce your body weight & slim your body Loss weight MCSA 0 07-21-2007 05:15 AM
all the text (including tags) between <body> .. </body> tarakparekh@yahoo.com Perl Misc 5 09-07-2005 11:40 PM
Not detecting body.scrollTop and body.scrollLeft in IE6 London Boy Javascript 2 01-12-2004 08:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57