• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

which comes first?

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What should be the sequence of execution of the following?
constructors
static initializers
static expressions
instance initializers
instance expressions
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find detailed explanations on the class initialization procedure in JLS 12.4 Initialization of Classes and Interfaces
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the order is
1)static initializers/expressions in the order they appear
2)instance initializers/expressions in the order they appear
3)constructor

bharat
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is good if you have no class hierarchy. But if you have super class: (assuming you are creating an object of sub class)
1)static initializers/expressions in the order they appear of the super class
2)static initializers/expressions in the order
they appear of the sub class
3)instance initializers/expressions in the order they appear of the super class
4)constructor of super class
5)instance initializers/expressions in the order
they appear of the sub class
6)constructor of sub class
 
reply
    Bookmark Topic Watch Topic
  • New Topic