aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question about constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question about constructor?" Watch "a question about constructor?" New topic
Author

a question about constructor?

feng lee
Ranch Hand

Joined: Oct 29, 2002
Posts: 33
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

Joined: Oct 31, 2002
Posts: 108
bcos bad programmers do not initialize instance variables in constructors.


SCJP 1.4<p>Wingardium Leviosa!!
feng lee
Ranch Hand

Joined: Oct 29, 2002
Posts: 33
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

Joined: Oct 23, 2002
Posts: 904
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

Joined: Jul 03, 2001
Posts: 2120
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 ]

SCJP2. Please Indent your code using UBB Code
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Posted by Jose:
Zero is the default value for all the types.

What about non-integral types?
Guy A. Buyle
Greenhorn

Joined: Nov 07, 2002
Posts: 14
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
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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

Joined: Aug 03, 2002
Posts: 7729
Kathy appended:
"4,000 year old object found in Las Vegas"
(from the National Enquirer)

Tom Jones?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

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

Joined: Oct 29, 2002
Posts: 33
Thank you very much!All of you are kind.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a question about constructor?
 
Similar Threads
how do variables get initialized when a blank constructor is provided
Default constructor ?
Java Quiz
of a default constructor question required
Default constructor