Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Doubt in initialization block.

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I got this question from Devaka Exam Lab (Final):



the output is "SCJP, Mobile Application Developer, null"

But, I had tried this in IDE,



The output is "41236577123657". The initialization block is compiled.

I just wonder why the initialization block does not compile? please help.

Many thanks,

Regards,
Edmen
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Does not compile" means that there is an error message when you run the compiler, and no class file is produced. That's not what happens here -- all of this code compiles.

The instance initializer block in the first example -- the one that tries to print "SCEA" -- will only be called if an instance of the class is created. Instance initializers run at the beginning of each constructor, immediately after any superclass constructors. In the "maClass" example, no instance of maClass is ever constructed -- i.e., there's no "new maClass()" anywhere -- so that initializer is never invoked.

In the second example, main() creates two instances of CDemo, and so the instance initializers (which print "1" and "3" in the super and subclass, respectively) will each run twice.
 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, is not "does not compile". I am sorry that I had mistaken.


The instance initializer block in the first example -- the one that tries to print "SCEA" -- will only be called if an instance of the class is created. Instance initializers run at the beginning of each constructor, immediately after any superclass constructors. In the "maClass" example, no instance of maClass is ever constructed -- i.e., there's no "new maClass()" anywhere -- so that initializer is never invoked.



I got it.

Many thanks.

Regards,
Edmen
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic