Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Question about order and initialisers

 
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The answer is



I'm was expecting

I understand first two give:





but then why



gives ucrcr should it not give ucrucr ?

thanks if you can understand what I am saying and explain
 
Rancher
Posts: 4614
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static initialiser is only run once, when the class is first loaded.
 
Niall Kev
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ok. So it runs the program: result is already

u

then it runs the constructors

cr

cr



Then you get:



and add the result ucrcr

?
 
Marshal
Posts: 69799
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

Do questions about the order of initialisers come up in the Associate exam? The gory details are in the Java® Language Specification starting with that section.
  • 1: At class loading time: the first static initialiser.
  • 2: Immediately after that: the 2nd static initialiser.
  • 3: Next: the 3rd static initialiser, etc etc
  • 4: Whenever an instance is created: The Object() constructor
  • 5: Immediately thereafter: the superclasses' constructors called via super(...); in order from nearest to Object.
  • 6: Thereafter: the 1st instance initialiser
  • 7: Next: the 2nd instance initialiser
  • 8: Following directly after: the 3rd instance initialiser etc etc
  • 9: Last but not least: The remainder of the current object's constructors, starting immediately after super();
  • Remember that the constructors can be called in different orders depending on whether they include this(...); Remember also that stages 4‑9 are called repeatedly, whenever new objects are created, and stages 1‑3 are called once, only at class loading time.
    At least I think that is correct.
     
    Marshal
    Posts: 25676
    69
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Where do the instance initializers of superclasses fit in that sequence?
     
    Campbell Ritchie
    Marshal
    Posts: 69799
    277
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think, after the super(); call executes their superclass' constructor, and before remainder of the constructor.
      Bookmark Topic Watch Topic
    • New Topic