Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Swati Kadam
Ranch Hand
Posts: 39
  • 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
  • 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
  • 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: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic