aspose file tools
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 Groovy Fundamentals video training course this week in the Groovy 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
Sheriff

Joined: Oct 14, 2005
Posts: 19553
    
  10

Simply put, your code doesn't create any instances of the Star class. Therefore that instance initializer block is never executed.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Block (not static)in a class...??