aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Code output -Doubt. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Code output -Doubt." Watch "Code output -Doubt." New topic
Author

Code output -Doubt.

Pinki Roy
Greenhorn

Joined: May 16, 2008
Posts: 22

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.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Please UseCodeTags I remember this being told for your previous post. You can always edit you post by clicking on and then wrap your code around the [ code ] tags.


Mohamed Sanaulla | My Blog
Zeeshan Sheikh
Ranch Hand

Joined: Nov 20, 2011
Posts: 144

Its basic Inheritence. Super class is A and intDemo is a subclass of A

1. The execution starts with main and simple output will print Hello 2

2. Next constructor of subclass is called in which object of class is instantiated. so Hello is printed

3. The first line in a subclass constructor is super() which is the call to super class constructor so Hello is printed

4. and eventually hello 1 is printed.

Hope this will help.


MySQL Blog
http://mysqlearner.blogspot.com/
Jozsef Toth
Greenhorn

Joined: Feb 16, 2012
Posts: 4
A ob=new A(); this creates a new instance of A, so running its constructor, this is the first Hello. (this runs because ob is an instance variable)
InitDemo constructor also calls super(), this is second Hello.
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

The second "HELLO" is because of in your InitDemo class.

When you instantiate an object of type InitDemo from within your main() what actually happens is that the data members are also instantiated with them ...
in your case as you have instantiated var "ob" it calls the constructor A() and hence "HELLO" gets printed ...


http://plainoldjavaobject.blogspot.in
Pinki Roy
Greenhorn

Joined: May 16, 2008
Posts: 22
Jozsef , thanks so much for the clarification.
harsh sahay
Greenhorn

Joined: May 13, 2011
Posts: 13

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.


Hi...as i understand an object is created when its constructor has completely executed along with a copy of its instance variable created. when control returns to InitDemo constructor from class A, only after the complete execution of the constructor will InitDemo object be formed and instance variable ob will be initialized to ob=new A() (if you go like this the output should be Hello2 hello hello1 hello);

Could someone please explain why A ob=new A() statement is getting executed before the completion of the InitDemo constructor??(as the output shows Hello2 hello hello hello1) which in turn means that InitDemo instance variable ob is getting initialized before the actual formation of the InitDemo Object as the constructor would not have completely executed by then??
confused between the last two outputs in bold...



OCPJP6
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
I have the same question as Harsh has.That, "why A ob=new A() statement is getting executed before the completion of the InitDemo constructor?"
and also what actually happens when we do " new InitDemo()". In what order a class runs?
Alex Theedom
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

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 - Senior Java EE Developer (OCPJP/OCPJWCD)
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Thank you Alex for your explanation.
Mezan Shareef
Ranch Hand

Joined: Apr 01, 2010
Posts: 40
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.



Thankyou. IT was very helpfull.
Alex Theedom
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

I am glad that the explanation was useful. Here are some notes I made when I studied this topic:






1. The control jumps to the Grandparent class and all static blocks are executed starting with the Grandparent, then Parent and finally the Child class.
2. Then the super() method is executed in Child’s constructor, which calls the Parent’s constructor, it’s super() method is executed which calls the Grandparent’s constructor and it’s super() method is executed which calls the empty constructor of the Object class.
3. Then the flow reverses down the tree hierarchy starting with the Grandparent class then the Parent class and finally the Child class.
4. As the control passes from one class to the next the follow things occur in this order:
a. Instance variables are given their values
b. Instance blocks are executed in the order they appear in the class.
c. The remaining unexecuted code in the constructor is executed.

Interesting Notes:

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.

If you change new Child() to Child c = null; the output will be:

Static Int: Grandparent
Static Int: Parent
Static Int: Child

This is because the static initiation blocks execute when the class is loaded, NOT when the object is created.

I hope this sheds some light on this topic.
harsh sahay
Greenhorn

Joined: May 13, 2011
Posts: 13

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.

Thanks for the above code Alex . After executing and debugging the above code and other examples, i think that the instance variables and initialization blocks are always executed before the constructor body runs(and not after executing the constructor as i was thinking it to be). So this doubt is now cleared.

I have also tried to execute the above two quoted notes:-
1.)The first one i tried.
2.)For the second one here is the code snippet:-


My Doubt here is:-

Shouldn't the compilation error occur at line Line 1 also?

I also noticed that the scope of variables defined and initialized in an initialization block is local to that block only(as can be expected from any other normal method).










Alex Theedom
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

What is happening here is as follows:

4. As the control passes from one class to the next the follow things occur in this order:
a. Instance variables are given their values
b. Initialisation blocks are executed in the order they appear in the class.

The instance variable i is defined and given its default value on line 11 (step 4.a) then the initialisation block is executed (step 4.b), on line 7 the variable i is ASSIGNED the value 2, on line 9 the variable is USED. There is a compiler error on line 9 because the following rule is applied: "The declaration of a member needs to appear textually before it is used" (http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#287406). The assignment on line 7 does not cause a complier error because its use is on the left had side.

Have a look at the following code snippets:

This code does not produce a code compiler error and outputs the value 10. The variable i is used on line 10 after it is declared on line 7. The declaration of the variable i (line 7) appear textually before it is used (line 10).

Have a look at this code:

The output is:

0
10

What if the variable is defined as final, can we assign a value on line 7 if its default value is 0? Yes we can because a final variable is not assigned a default value, it must be initialised before use. So a compiler error occurs on line 6 because the blank final field i has not been initialised.

I think I am diverging from the original post....anyway an interesting little diversion, I don't know if this type of thing will be tested on the exam but it has all the hallmarks of the kind of tricky little detail that the examiners are famous for trying to "trick" you with, so perhaps worth remembering just in case.




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code output -Doubt.