• Post Reply Bookmark Topic Watch Topic
  • New Topic

When exactly is a class called in Java?  RSS feed

 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While reading about the Static initialization blocks, I learned that it is executed when the relevant class is called. Exactly when does that happen in the line of execution of a typical code?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classes aren't called, but loaded. Whenever a class is loaded into memory, its static initialisers are executed. A class is loaded not later than the first time any code mentioning it is executed (maybe earlier). I don't think it is possible to be more precise than that.
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Classes aren't called, but loaded. Whenever a class is loaded into memory, its static initialisers are executed. A class is loaded not later than the first time any code mentioning it is executed (maybe earlier). I don't think it is possible to be more precise than that.


Please tell me if I am wrong here, I think that typically it would then happen when an object of that class, is created in the 'Main' method.
 
Arco Brouwer
Ranch Hand
Posts: 44
2
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:... A class is loaded not later than the first time any code mentioning it is executed (maybe earlier)...


What about import statements, are they also loading classes into the memory, or is it just a reference to the class location?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imports are only significant at compile time.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Bhatt wrote:
Please tell me if I am wrong here, I think that typically it would then happen when an object of that class, is created in the 'Main' method.

There is nothing special about the main() method here. A class will be loaded no later than the first time that class is referenced during program execution from any method of any other class, or from any initialization within any class. There is no need for that to occur in the main() method.

Also, imports are there only so that the compiler can determine the correct code to generate (for example, ensuring that any methods you call from a particular class actually exists).
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Bhatt wrote:. . . when an object of that class, is created in the 'Main' method.
But you shouldn't be creating lots of objects in the main method in the first place. Look at our FAQ. Once the application is running other code will be executed and that code will mention many different types. Each of the classes corresponding to those types will be loaded in time for their use; that is when the static initialisers will be run. You cannot tell how much earlier each class will be loaded. That will vary from implementation to implementation and maybe even from run to run because different runs will use different parts in different orders depending on user input.
If I click the create document button on your GUI, different classes will be loaded than if I click the retrieve saved document button.
 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static initializers are executed when the class gets initialized, not when it gets loaded. A class can be initialized by accessing a field (that is not a compile time constant), invoking a method, calling Class#forName, etc..., for a more complete list see JLS 12.4.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!