Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

Initializer vs. Constructor assignment

Thread view: 
Matthias Kaeppler - 07 May 2005 16:07 GMT
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?

Signature

Matthias Kaeppler

SPG - 07 May 2005 18:01 GMT
> Hello,
>
[quoted text clipped - 29 lines]
> 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
Tor Iver Wilhelmsen - 07 May 2005 18:04 GMT
> class A {
>     int a = 5;
[quoted text clipped - 8 lines]
>     }
> }

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.
Viator - 07 May 2005 22:32 GMT
Hi!
My friend is not in consent with you. Are you sure about the "new"
stuff?

Rgards,
viator
Tor Iver Wilhelmsen - 08 May 2005 07:43 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.