On moving the declaration of Test below map, it runs
SCJP 6, SCWCD 5, SCBCD 5
Lee Kian Giap wrote:
Now, I bring out a good practice which one need to follow is , "In a constructor, you can call superclass method, because superclass surely is completely loaded and initialized. However, in a constructor, one should not (although you can) call method from its own class because it will give a non guarantee behavior where its own class haven't completely initialized"
All The Best, hope my explanation help you to progress.
Thanks, Now I understood how static declarations are instantiated in the first place. To just check if I understood correctly or not, I made the Test declaration in it non static and removed the getInstance() method and ran it. Now whatever may be the order it prints the values as static objects are instantiated first. Only in the previous case since both were static it followed the order of declaration
One question, you said it better to always call the superclass from the constructor.
By default the default superclass constructor is called. Also the object I want to refer or load when the object is instantiated is within that class and not the super class. So I guess it makes sense to call it from the constructor, or have that piece of code within the constructor itself. How does having superclass solve it. Do you say, I must put the static variable in the superclass ?
SCJP 6, SCWCD 5, SCBCD 5
No, by default a superclass no-arguments constructor is sought. It is invoked if it exists. If not, you must provide a super( . . . ); statement.kalyana cj wrote:. . . By default the default superclass constructor is called. . .