• Post Reply Bookmark Topic Watch Topic
  • New Topic

not able to understand the output of the following piece of code?  RSS feed

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



this gives the output as
1
2
hi
3

I have read alot about object initialization and static initialization. read quite some articles but have got confused. it would be really helpful if somebody explains the control flow, as well the concept for the above code. been working for 2 days and still confused ? thanks
hi
 
ronney singh
Ranch Hand
Posts: 40
IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this gives the output as
1
2
hi
3



Are you sure this is the output of your code because there must be one more "hi" after "3".

So please run your code again.

In main function of your SIB class you are making an object of class B, that will run first static initializer of class.
As class B is extending class A so first static initializer of A will run that will print "1", then static initializer of class B will run and print "2".

After static initializer, constructor will run, same as class B is extending A so first constructor of A will run before constructor of class B which will print "hi" and after that "3" in next line

Here you have to check that you are making an object of class A, so it must print "hi" in the output.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This shows the order in which static initializer blocks, instance initializer blocks and constructors are executed.

Static initializer blocks are executed when a class is initialized (immediately after the class is loaded). The "1" comes from the static initializer block of class A and the "2" comes from the static initializer block of class B. Class A is initialized before class B, because class B extends class A (so, class B needs class A).

In line 21 you are creating a new instance of class B. Since class B extends class A, a B object consists of an A object plus some extra stuff. When the B object is created, first the A part of the object is initialized. Part of this initialization is calling the instance initializer of class A (line 3). That's why you see the "hi" printed. After the A part is initialized, the constructor of B is called. That's why you see the "3" printed (line 14).

You forgot something in the output. In line 22 you create an A object. As part of that, the instance initializer block (line 3) is called again. The actual output of running the code is:

1
2
hi
3
hi
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us where that code comes from.
Please format the code so we can actually read it; in that instance the code tags simply accentuate how badly you have indented the code.
No, we don’t tell you that sort of answer. We expect you to work it out and show us what you think and we shall tell you whether we think it is correct.
 
Pondurai Singh
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So it prints 1

So it prints 2.

So it prints Hi.

So it prints 3.

So it prints Hi.

Note:static initi block Executed once when the class is loaded
 
Bill Johnston
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ronney singh wrote:

this gives the output as
1
2
hi
3



Are you sure this is the output of your code because there must be one more "hi" after "3".


FYI ... The last "hi" is at the end after "thanks"
 
ronney singh
Ranch Hand
Posts: 40
IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this gives the output as
1
2
hi
3

I have read alot about object initialization and static initialization. read quite some articles but have got confused. it would be really helpful if somebody explains the control flow, as well the concept for the above code. been working for 2 days and still confused ? thanks
hi

Thanks Bill I found out "hi" here.
Please gurpreet, never show your outputs the way you did here. First check them properly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!