This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How java programs work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How java programs work" Watch "How java programs work" New topic
Author

How java programs work

Momen Travolta
Greenhorn

Joined: Mar 19, 2010
Posts: 8
Just few questions about how Java programs work:-

1)Are classes loaded to memory even if they are not instantiated yet? (I think yes)
2)When are classes loaded? on demand? or once the app. is executed?
3)I think contructors are for constructing new objects, while classes are loaded to memory without the need for constructors (even implicit constructors.) Is that correct?
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
1)Are classes loaded to memory even if they are not instantiated yet? (I think yes)
2)When are classes loaded? on demand? or once the app. is executed?

I would say/guess that the classes are loaded at the latest when they are needed
by the virtual machine at runtime/execution. (You can use a class that is never instantiated,
e.g. static methods.)
3)I think contructors are for constructing new objects, while classes are loaded to memory without the need for constructors (even implicit constructors.) Is that correct?

Constructors are used when creating objects from classes.
I believe classes can have static construcors initializers that run when the
class is loaded.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14953
    
  26

Jonas Isberg wrote:I believe classes can have static construcors that run when the class is loaded.

A class can have a static initializer (it's not called a "static constructor") that's run when the class is loaded and initialized:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
Thanks Jesper for correcting my confused terminology.
 
Don't get me started about those stupid light bulbs.
 
subject: How java programs work
 
jQuery in Action, 3rd edition