| Author |
Q of Dan's Mock Exam
|
Claire Yang
Ranch Hand
Joined: Aug 30, 2002
Posts: 57
|
|
Hi, I modified one of Dan's Mock Exam questions to let it run completely. My question is why it prints "2513469" since the code doesn't call the constructor R that includes an int parameter? Thanks in advance. PS. How to use FONT tag to let the code be showed bigger? I tried but failed. [ October 28, 2002: Message edited by: Claire Yang ]
|
 |
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
|
|
In this code: the body of the constructor is {} -- that is, empty. The System.out.print("9"); statement is not part of the constructor. It is an instance initializer and will always run.
|
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
|
 |
Jay Ashar
Ranch Hand
Joined: Oct 13, 2002
Posts: 208
|
|
|
If it is an instance initializer shouldnt it run before constructor runs??
|
SCJP 1.4<br />SCWCD 1.3
|
 |
Jessica Sant
Sheriff
Joined: Oct 17, 2001
Posts: 4313
|
|
Originally posted by Claire Yang: PS. How to use FONT tag to let the code be showed bigger? I tried but failed.
can't be done -- HTML has been disabled in this forum. The [code] blocks come in one font only. sorry!
|
 |
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
|
|
It does run before your constructor does (but not before the superclass constructor does). Note that your constructor body is also {} -- it prints out nothing. {System.out.print("6");} is another instance initializer. [ October 29, 2002: Message edited by: Ron Newman ]
|
 |
Claire Yang
Ranch Hand
Joined: Aug 30, 2002
Posts: 57
|
|
Hi, thank a lot. I know where I was wrong now, I should put the println statement into the block of the constructor. Sorry, a "mistaken" question!
|
 |
 |
|
|
subject: Q of Dan's Mock Exam
|
|
|