This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.
Joined: Oct 31, 2002
thank,alberto . I am sorry but I can't confirm which is the right answer,please tell me.
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 ]
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....
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?
Joined: Jul 03, 2001
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.
Joined: Jul 02, 2002
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 ]