Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q of Dan's Mock Exam

 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is an instance initializer shouldnt it run before constructor runs??
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic