This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Class Loading Doubt.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Loading Doubt.." Watch "Class Loading Doubt.." New topic
Author

Class Loading Doubt..

Clarence J M Tauro
Ranch Hand

Joined: Aug 09, 2008
Posts: 30
Let us consider the following code with two different classes:



When I just create a reference in "main", will the Employee class be LOADED?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

When you create a reference to an Employee object, yes, the Employee class will be loaded. But the code you posted does not create a reference. It simply declares a variable of Employee type, and that doesn't cause the Employee class to be loaded.

Here's some code which creates a reference:
Clarence J M Tauro
Ranch Hand

Joined: Aug 09, 2008
Posts: 30
Thanks Paul;

Now, which means when I just do



the class does not get loaded, and static initilizer/block does not get called right. I also verified the same.

Now can I have the static block of Employee class executed without creating the instance of the class or accessing some static method or variable of Employee class? I mean to say, how do we load the class directly?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Clarence J M Tauro
Ranch Hand

Joined: Aug 09, 2008
Posts: 30
Thanks Paul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class Loading Doubt..
 
Similar Threads
Employee with same values is being added two times in HashSet
Comparable and Comparator
Sorting arraylist on the basis of object
how comparator works?
hashcode and equals