Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Block (not static)in a  class...?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Block (not static)in a  class...??" Watch "Block (not static)in a  class...??" New topic
Author

Block (not static)in a class...??

Swati Kadam
Ranch Hand

Joined: Mar 05, 2008
Posts: 39
class Star {
static {
System . out . print ( " Static " ) ;
}

{
System . out . print ( " Init " ) ;
}

Star ( ) {
System . out . print ( " Const " ) ;
}

public static void main ( String args [ ] ) {
System . out . print ( " main " ) ;
}
};

In this method Static and main is printed...
why init is not displayed???
Even if we create construtor of the class Star init is not printed? why is it so?


SCJP / Preparing For SCWCD
Phani Burra
Ranch Hand

Joined: Dec 13, 2007
Posts: 52
An init block runs everytime an instance of the class is created.
Swati Kadam
Ranch Hand

Joined: Mar 05, 2008
Posts: 39
Originally posted by Phani Burra:
An init block runs everytime an instance of the class is created.


If it runs for every instance then why it is not printing Init in the above code?
i am still not getting it...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 19357
    
  10

Simply put, your code doesn't create any instances of the Star class. Therefore that instance initializer block is never executed.
 
Have you tried LearnNowOnline? http://www.learnnowonline.com/
 
subject: Block (not static)in a class...??