• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructors  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Just {
int a = 10 ;


Just ( ) {
call ( ) ;
}

void call ( ) {
System . out . print( "a = " + a + " ") ;
}
};

class Q05 extends Just {
int b = 16 ;

Q05 ( ) {
call ( ) ;
}

void call ( ) {
System.out.println("i am here");
System.out.println( " b = " + b + " " ) ;
}
public static void main ( String args [ ] ) {
new Q05 ( ) ;
}
}

Ans: b=0 b=16

Please explain how b=0?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets see...

void call ( ) is an overridden method. Object Type determines which overridden method is called at runtime.

Because the object created is the Q05 child object...both the parent constructor and the child constructor calls only the child objects call method!! Hence the result!!

- Mel
 
Melanie Jones
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables are initialized only after the corresponding constructor completes execution...So I guess b=0 is because of the default value of int instance variable!! Not sure if this makes sense?

- Mel
 
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This are steps taken when a new instance of a class is created according to the Java Language Specification.

1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
2. If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.


So I believe Melanie is correct. Because the no-argument constructor of the superclass is invoked first before the instance variable initializers are executed, and since the method call() is overridden by the subclass, the method is called, but b has the default value of 0.
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a variation on the above code, with more detailed output to show what's going on...

Note that instance variables are set after the constructor's implicit call to super(), but before the rest of the constructor body executes.
[ February 21, 2006: Message edited by: marc weber ]
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very good question.

At a first look I also got this wrong.

Thanks Melanie for the briefly and clear explanation.
 
ABISHEK BABU
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!