• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt about loading classes

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


/*
Output:


When Mark 1(new Z()) is not commented output: YXYZ
When Mark 1 is commented output: (program runs without printing anything on screen)

My Understanding(I am not confident about this):

(1) When new Z() is commented: When we compile the code and run the code no class is loaded and hence no constructor or class code runs.
(2) When new Z() is not commented: When we compile the code: syntax is ok so it compiles successfully. When we invoke java then the main () method is run and constructor is called on Z which loads the class associated with it and hence the output.
*/

I thought that output would be YYXZ when new Z() is not commented. I was of the notion that when I invoke java nitIon2 then class X, Y and Z would be loaded automatically and then the statement new Z() would be executed and hence the output would be YYXZ.
However, it is not so.
Request you to please clarify my doubts regarding the compiling, executing, and loading etc(or whatever it is which I don't know right now) by throwing some information in a step by step manner right from beginning with the compilation of a .java file.
It would be really helpful of you.

Best Regards,
Nitin
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should read this: Class Initialization (JLS) and Class Instantiation (JLS)

A class is loaded basically the first time the class is referenced. Loading a class consists of creating the static context of the parent class, then building the static content of the class in question.
1) Lock the class
2) load the parent class - recursively run these steps on the parent class
3) assign static members and run static initializers

When the class is instantiated, and instance of the class is created, then:
1) The parameters to the constructor are processed
2) The parent constructor is called -> recursively applies these steps to the parent class
3) Instance members are created and initializers are run
4) The body of the constructor runs.

So when you run Class Z here is what happens:

Now Z is properly loaded, so main() can execute. When Z.main() is executed, you call new Z() so you go through the process of making a new instance of Z:
 
Always! Wait. Never. Shut up. Look at this tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic