File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Swati Kadam
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Phani Burra
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An init block runs everytime an instance of the class is created.
 
Swati Kadam
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic