jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Class loading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class loading" Watch "Class loading" New topic

Class loading


Joined: Sep 17, 2004
Posts: 7
Does a non-static method gets loaded into the memory, when a static method is called on the class (assumption is that there are no objects instatiated)?
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
hi sreedhar ,

your question is not very clear to me. however, i am giving the following explanation,I guess it helps you.

static methods belong to the class and not any instance of the class, static code in a class is executed only once, when the class is initialized. and unless u create an instance, ie.. an object, u cannot use the non-static methods of the class. Anything in a class that is declared static is executed when the class is loaded.

I hope it helps..

Impossible is I M Possible
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
you might like to use the -verbose option . try to use it and go through the output that this option gives you..

all the best ..
marc weber

Joined: Aug 31, 2004
Posts: 11343

The JVM loads a class either when the first instance of that class is created or the first time a static member of that class is referenced (even if an instance is never actually created).

So if there are no instances of that class, then calling one of its static methods will load the class.

When a class is loaded, the entire class file is loaded. At that time, static variables are initialized and static blocks of code are executed. Non-static members are initialized only if an instance is created -- but, again, the entire class loads at one time.
[ September 18, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

We request that display names follow the pattern FIRST_NAME + SPACE + LAST_NAME.

Thanks Pardner! Hope to see you 'round the Ranch!

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Intermediate forum...
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
Loading is the first step in any execution. So when JVM loads the class it won't analyze to see what method is called and what isn't. It will load the entire class definition including static + member methods. Hope this answers your question.
I agree. Here's the link: http://aspose.com/file-tools
subject: Class loading
It's not a secret anymore!