• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Code confusion

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,I am new to this forum.
Please check the following code:



The output is as follows:


Now my question is why
"trim"
and "toppercase"
are not printed first, when they are the first two logics in the code?
Please help me out with the explaination

Thanks and regards.

Hirak
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once a class is loaded in memory its static block and static members are first inialized and executed.
So when JVM invokes main() of child Child.main(), First Child methods static block and static variables are initialized but since Child class does
not have both nothing happens and since Child class extends Parent class, Parent class is loaded in memory and static block and static variables are
initialized;


First the parent class is loaded in memory and
1) Parent class static block is executed first
2) Static variables in class are initialized
So now the first statement to exceute is:
O/P1: "Parent Class Instance Initializer"
Now the parent class static memeber is intialized
static int bb = getAA();
So the second statement to be executed is the getAA() which displays
O/P2: "Parent Class Member Initializer:static"
3) In child class main() if statement is executed which results in true and hence System.out.println("trim");
O/P 3: trim
4)Second if statemen runs and displays "toppercase"
O/P 4: toppercase
5) Now Child c = new Child()
Now this will first invoke Child() which invokes Parent() through a implicit
super() in Child() as the first statement, So now once in Parent() all the
instance blocks and instance variables are executed and initalized , So now
the parent class a=getA() is invoked which displays and there is no instance blocks
O/P5: "Parent Class Member Initializer"
And onnce the members are initalized the statments in Parent() is executed
which is "Parent Class Constructor"
O/P 6: "Parent Class Constructor"
And it returns to Child class,
Again same logic as above is applied
O/P 7 : "Child Class Instance Initializer"
O/P 8: "Child Class Member Initializer"
O/P 9: "Child Class Constructor"
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooof hope this explains
Thanks
Deepak
 
hirak chatterjee
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was a very helpful information.Thank you.Things are getting clear now.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good explanation Deepak Jain. And a good example you have posted hirak chatterjee.

What exactly is the instance block? I know the static block. Can you please throw some more lights on this Deepak Jain?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!

Raghavan asked:

What exactly is the instance block?



An instance block is for code that shall be the same for all constructors.
It will be executed after the call for super but before the rest of the constructor runs.

When you have overloaded constructors, then you could put the code to be shared into the instance block.
Example:
prints:
fish constructor
Instance Block providing Nemo with 3 stripes.
nemo constructor with int
nemo constructor parameterless
Nemo with 6 fins and 3 stripes.
fish constructor
Instance Block providing Nemo with 3 stripes.
nemo constructor with int
Nemo with 7 fins and 3 stripes.


The two constructors share the instance block, so every Nemo got 3 stripes and we get an output of this.





Yours,
Bu.
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic