permaculture playing cards
The moose likes Beginning Java and the fly likes Loading a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loading a class" Watch "Loading a class" New topic

Loading a class

Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62

I am reading about static initialization blocks. These get executed when a class is loaded. When does a class get loaded?

For example,

When does the class Animal get loaded?

Thanks for help in advance.

James Dixon
Ranch Hand

Joined: Jun 20, 2009
Posts: 32
Hi Anu

The best way to find out would be to put some break points in the code (for example, in the constructor for Animal and in the UseAnimal 'main' method) then debug your application.

that should give you some idea of the order in which things are initialised

have fun!
shukla raghav
Ranch Hand

Joined: Aug 03, 2008
Posts: 203
classloader is a componenet of the JVM that loads the class files obtained after successful compilation.after compilation you can find your class files in the bin folder. when we run a java program it first loads these .class file that contain the bytecode understandable to the JVM. To further clear your point LOADING here means reading the class files from the bin folder to your memory (primary) where execution is performed. Now static variables are created at this moment (when the class is loaded). But non static (instance/local variables) can only be created (allocated memory space) at the time of execution when the object instantiation is performed. Static variables do not require explicit instantiation. The moment class files are loaded static varibales and static blocks also are allocated memory space.

Next is the process called as linking - here the loaded .class file is converted into ready to execute state. Like different thread schedules are created, stacks are created and pushed, and heaps are generated. (read stacks and heaps)

Then come the execution phase where stacks are executed one by one. It is in this phase that the Objects and instance variables are created.So we can see how early the static variables are created as compared to instance variables or objects.

Specifically if you wish to know why we need Static blocks. Supposedly in a situation we wish to create a certain set of static data for sharing data amongs different object - but the static data is not to be initialized simply by initialization statement rather it requires some sort of processing like conditions for assessing the value to be assigned. in such a case we cannot create a method as a method can only be executed after a class is instantiated. In such a case we may use a static block to instantiate the data. more specifically a static block is used when we need to process certain values before being used by the constructor.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 1109

I would prefer Sysouts or better yet, Logger statements.

I agree. Here's the better link:

Mark Toddd

Joined: Jan 05, 2010
Posts: 11
I found this article, it might be useful to you.

Java Class Loading: The Basics -

"What you talkin' bout Willis?"
I agree. Here's the link:
subject: Loading a class
It's not a secret anymore!