Forums Register Login

a question about constructor?

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
bcos bad programmers do not initialize instance variables in constructors.
+Pie Number of slices to send: Send
I mean when you invoke constructor,it will automaticly initialize all the instance variables.eg:int i; i will be initialized as 0.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Posted by Jose:

Zero is the default value for all the types.


What about non-integral types?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
Kathy appended:

"4,000 year old object found in Las Vegas"
(from the National Enquirer)


Tom Jones?
+Pie Number of slices to send: Send

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 ]
+Pie Number of slices to send: Send
Thank you very much!All of you are kind.
pie. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 771 times.
Similar Threads
Default constructor
how do variables get initialized when a blank constructor is provided
Java Quiz
of a default constructor question required
Default constructor ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:24:43.