aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about default constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about default constructor" Watch "question about default constructor" New topic
Author

question about default constructor

dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
please explain to me which is right about the two options?and tell me the reason,thanx.
C. The default constructor invokes the no-parameter constructor of the superclass.
D. The default constructor initializes the instance variables declared in the class.


scjp 1.4<br />challenge haven't limit!
alberto ramos
Greenhorn

Joined: Sep 23, 2002
Posts: 7
Every object belongs to a hierarchy of objects. The object from the Object class is the superclass of all objects. To construct an object from a class that does not define any constructors, a default constructor is created; and it is this constructor, the one in charge of calling the superclass.
When a new object is created initialization happens in this order:
1. static statements/blocks, IN THE ORDER they are defined.
2. instance initializer/blocks IN THE ORDER they are defined.
3. Finally, the constructor is called.


SCJP2
dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
thank,alberto .
I am sorry but I can't confirm which is the right answer,please tell me.
alberto ramos
Greenhorn

Joined: Sep 23, 2002
Posts: 7
Option D.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Bievenido al Rancho de Java Alberto.
Welcome to javaranch.

The compiler will generate a default constructor for each class with no constructor declared by the programmer. It has no arguments, the same access specifier as the containing class.
The first instruction in its body is super(). The intention of this is to ensure the call to the proper chain of constructors to initialiaze the instance fields declared in superclasses before the ones declared in subclasses.
After "super();" the compiler places the code that initializes instances variables of this class, and the code within the instance initializers blocks written by the programmer. Both are placed in textual order, that is in the order in which they appear in the source code.
You can check this using "javap -c Myclass"
Thus both C and D are OK.
[ December 01, 2002: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Alberto, I think you missed on this one.. option C is the right answer. The implicit or explicit default constructor implicitly - if not explicitly - calls the default constructor of the direct super class and thus option C is right.
Default constructor doesn't initialize the instance variables, they're initialized to their default values even before any statement in the default or non-default constructor - other than the super() or this() calls - are executed.
Anyone correct me if I'm wrong....


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
alberto ramos
Greenhorn

Joined: Sep 23, 2002
Posts: 7
After re-reading the question, I realize that D is wrong, because it is not the constructor that initializes the variables. So now I think the correct answer is C.
Gracias Jos´┐Ż.
Thank you Alfred.
dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
thanx,guys!
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
Originally posted by ji dragon:
please explain to me which is right about the two options?and tell me the reason,thanx.
C. The default constructor invokes the no-parameter constructor of the superclass.
D. The default constructor initializes the instance variables declared in the class.

my two cents worth:
I'd agree that C. is the only correct answer for sure (without any other details to go by)
example:

No instance variables appear in Dog or Object classes.
as for " D. The default constructor initializes the instance variables declared in the class."
I don't believe that the default no-arg contructor for Object initializes anything and I have not overrided it in Dog, so all that one can do is call super() implicitly.
I suppose I could unpack the code for the constructor in the Object class ???
[B]Am I correct?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
There a powerful tool to check this kinds of doubts. "javap -c Test"

You can see that the code that initializes the instance fields is placed by the compiler within the constructor.
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
Jose,
I missed the last line in one of your previous post which said " You can check this using "javap -c Myclass" "
Now it is very clear!
C and D - thanks!
[ December 02, 2002: Message edited by: david eberhardt ]
dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
thanx,Jose!
 
Don't get me started about those stupid light bulbs.
 
subject: question about default constructor