This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question about constructor?

 
feng lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is true about constructors ? (Select two)
a) They initialize all the member variables
b) They initialize all the instance variables
c) If there is no constructor , the compiler includes default
constructor.
d) Compiler supplied constructor will invoke the default constructor of the super class.
An:c d
why b is wrong?
 
Don Bosco
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bcos bad programmers do not initialize instance variables in constructors.
 
feng lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean when you invoke constructor,it will automaticly initialize all the instance variables.eg:int i; i will be initialized as 0.
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi feng
Not quite. When an class - or object- is loaded what happends is that
1. the static members gets initialized (default values are applied if they arn't explicitly given)
2. instance members are initialized
3. constructor gets called.
So the static/instance members get's called before the constructor.
Take a look at the following example:
(1) gets the default value 0, then the same happends at (2) and finaly (3) gets executed.

/Svend Rost
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance fields are initialized to their default values before the constructor is called.
The new operator allocates space enough in the heap for the instance fields declared in the class, and for those inherited. Then wipes out this space to zero. Zero is the default value for all the types. Then new returns the reference to this space, and the constructor gets invoked on this reference. This reference is "this".
The constructor initializes the instance fields to the values desired by the programmer, if any.
[ December 04, 2002: Message edited by: Jose Botella ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posted by Jose:
Zero is the default value for all the types.

What about non-integral types?
 
Guy A. Buyle
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The order of constructor calls for a complex object is as follows:
1. The storage allocated for the object is initialized to binary zero before anything else happens
2. The base-class constructor is called. This step is repeated recursively such that the root of the hierarchy is constructed first, followed by the next-derived class, etc., until the most-derived classis reached.
3. Member initializers are called in the order of declaration.
4. The body of the derived-class constructor is called.
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thought I'd take a shot at making it even *more* confusing
To add a little more...
You have three types of initialization for your instance variables, during object creation:
(1) You declare them but don't initialize them at all
int i;
Foo f;
(2) You declare them and provide explicit initialization at the time you declare them:
int i = 3;
Foo f = new foo();
(3) You EXPLICITLY initialize them within your constructor, IN CODE:
public class Bar {
int i;
Foo f;
public Bar() {
i = 42;
f = new Foo();
}
}
===========================
It is NEVER correct, for the purposes of the exam, to say that "instance variables are initialized in the constructor" because even though both version 1 (declare but never initialize) and version 2 (declare and explicitly initialize) WILL result in instance variable initialization, we can say that it happens DURING the construction process, but not necessarily IN the constructor. Only version 3, where you explicitly assign values inside the constructor -- in other words, IN CODE-- can be considered "variables initialized in the constructor".
So what DOES happen in those three versions?
Version 1: Declare without initializing ( int i; )
When the constructor is first invoked, the variables get their default values (but remember, we can't say that it happened IN the constructor), and they have those default values by the time the super constructor is invoked.
Version 2: Declare and explicitly initialize ( int i = 3; )
This one's a little dicier, because it's a two-stage process...
A) the variables are first given their default values (so int i == 0), and THEN the super constructor is invoked.
B) The variables are given their *real* values (the ones you explicitly assigned at the time you declared them, i == 3) only when ALL super constructors have finished, but just BEFORE the second statement in the original constructor (the one we said "new" on) runs
So basically:
* Before super constructors have run, instance variables have their default values (0, null, false, etc.)
* AFTER super constructors have fun, but before the second statement in the original constructor, variables have their *explicit* initialization, if you explicitly assigned values to them at the time you declared them.
Now, if you used version 3, and initialized them yourself in the constructor, then they happen exactly when they happen -- which at the least means AFTER the super constructors have run.
Cheers,
Kathy
"4,000 year old object found in Las Vegas"
(from the National Enquirer)
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kathy appended:
"4,000 year old object found in Las Vegas"
(from the National Enquirer)

Tom Jones?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here we see that the initialization type 2 really happens within the constructor.
Summing up: I think the sequence given by Kathy in my last quote (begining "so basically:") matches perfectly my previous post. In fact, I paid great attention to remark default
_________________________________________________


Zero is the default value for all the types.

Well obviously, all of us know the default Java value for reference variables. I just thought that because the memory is wiped out to zero, the real content of the position of memory corresponding to a reference variable initialized to null is zero.
Kathy, can you confirm that?
Ayway, I guess the quote is at least confusing.
[ December 07, 2002: Message edited by: Jose Botella ]
warning: my last edition seems to have deleted the begining of my text. Anyway the essential is still left.
[ December 08, 2002: Message edited by: Jose Botella ]
 
feng lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!All of you are kind.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic