Re: Initializer vs. Constructor assignment




"Matthias Kaeppler" <nospam@xxxxxxxxxxxxxxx> wrote in message
news:d5iljv$qav$03$1@xxxxxxxxxxxxxxxxxxxx
> 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?
>
> --
> Matthias Kaeppler


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


.



Relevant Pages

  • Re: Build-in types initialization
    ... user-defined types inferior in some way to built-in types. ... If I initialize coordinates to zeroes in default constructor, ... The default constructor for int, float, bool initializes them to 0 /false. ...
    (comp.lang.cpp)
  • Inheritence problem
    ... I have a base class called Animal and in its constructor I initialize a ... int Animal::getY ... try to debug I notice that once it leaves the Animal Constructor, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Constructors: Help me answer students question
    ... A constructor *is* a function. ... Also, 'C1::init' doesn't really initialize 'i', it ... the derived object. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: structure vs variable
    ... > int i;? ... Obviously you can write a constructor for a struct. ... but at least you could initialize it to 0 automatically. ...
    (comp.lang.cpp)
  • Re: Problem with linker
    ... but to have actually written a default constructor. ... such as overloading on int and pointer types. ... conversion of 0 to CString requires a user-defined conversion, ... acceleration operator *(distance d, time_squared t2); ...
    (microsoft.public.vc.mfc)