Difference between revisions of "Instantiation"
From Suhrid.net Wiki
Jump to navigationJump to searchLine 7: | Line 7: | ||
* '''CANNOT call an instance method or access instance variables till the super constructor has run.''' | * '''CANNOT call an instance method or access instance variables till the super constructor has run.''' | ||
* Only Static variables can be accessed as a call to super() or this(). | * Only Static variables can be accessed as a call to super() or this(). | ||
+ | |||
+ | == Initialization Blocks == | ||
+ | |||
+ | * A static initialization block runs when the class is first loaded.(Runs only once) | ||
+ | * An instance init block runs every time a new instance is created. | ||
+ | * '''An instance init block runs right after the call to super() in a constructor''' (ie. after all the super-constructors have run). | ||
+ | * In case of multiple init blocks (either instance and static) they run in the order in which they are defined. |
Revision as of 06:56, 16 August 2011
Constructors
- Every class - including Abstract classes - MUST have a constructor.
- Constructor chaining is built in. Every class constructor will call its default superclass constructor using super().
- Every constructor as its first statement has a call to this() or super().
- Compiler will only automatically insert call to no-arg super() or this().
- CANNOT call an instance method or access instance variables till the super constructor has run.
- Only Static variables can be accessed as a call to super() or this().
Initialization Blocks
- A static initialization block runs when the class is first loaded.(Runs only once)
- An instance init block runs every time a new instance is created.
- An instance init block runs right after the call to super() in a constructor (ie. after all the super-constructors have run).
- In case of multiple init blocks (either instance and static) they run in the order in which they are defined.