Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
MySQL Blog
http://mysqlearner.blogspot.com/
http://plainoldjavaobject.blogspot.in
Pinki Roy wrote:
I don't understand why the out put is
Hello 2
Hello
Hello
hello 1
As per my understanding it should have been
Hello 2
Hello
hello 1
Someone please clarify please.
OCPJP6
Alex Theedom. Author of: Java EE Video Courses (JAX-RS, Bean Validation, Websocket, JSON-P and much more), Java EE 8: Only What's New
and Professional Java EE Design Patterns. Blogger at readlearncode.com. Twitter @alextheedom
Alex Theedom wrote:The order in which the parts of the code are executed after the new InitDemo() is as follows:
1. When the "new InitDemo()" is executed a new object is being constructed so the constructor in line 8 is called,
2. this constructor then calls the super constructor in the A class, line 2;
3. the super constructor completes and returns control to the InitDemo class in which the instance variable is initialised,
4. a new A class is constructed in line 7 and the constructor of the A class is called again,
5. then the control passes to line 9 and the rest of the construtor of class InitDemo is executed.
It is true that the control jumps around a bit and can make you feel a bit dizzy at first, but there is a strict order of execution that is followed each time a class is instantiated. Also dont forget to included initialiation blocks (static and non static) in the execution order, they make it really good fun.
Alex Theedom. Author of: Java EE Video Courses (JAX-RS, Bean Validation, Websocket, JSON-P and much more), Java EE 8: Only What's New
and Professional Java EE Design Patterns. Blogger at readlearncode.com. Twitter @alextheedom
Instance blocks can give value to instance variables;
Instance blocks can use instance variables, if they are defined before the instance block in the code.
OCPJP6
Alex Theedom. Author of: Java EE Video Courses (JAX-RS, Bean Validation, Websocket, JSON-P and much more), Java EE 8: Only What's New
and Professional Java EE Design Patterns. Blogger at readlearncode.com. Twitter @alextheedom
Seriously? That's what you're going with? I prefer this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|