• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Code output -Doubt.

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 144
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...

 
Pinki Roy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jozsef , thanks so much for the clarification.
 
Greenhorn
Posts: 13
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Author
Posts: 116
11
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Alex for your explanation.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Author
Posts: 116
11
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Author
Posts: 116
11
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
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
reply
    Bookmark Topic Watch Topic
  • New Topic