• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Q of Dan's Mock Exam

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it is an instance initializer shouldnt it run before constructor runs??
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic