• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 28416
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic