wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes loading the class and initialization of class 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 "loading the class and initialization of class" Watch "loading the class and initialization of class" New topic
Author

loading the class and initialization of class

jazy smith
Ranch Hand

Joined: Nov 18, 2009
Posts: 101
Hi all,

I have few doubts. Please correct me if I am wrong.

(1) " I've read so many forums and they say Class.forname("X") will load the class X into memory or register your class. In javadocs, they say a call to forName("X") causes the class named X to be initialized. " in this context, initialization of class is same as loading the class into memory ?

(2) invoking the class means creating the instance (object) of the class.

Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
It means that the class will be loaded into cache and the static initializers will run.


JDBCSupport - An easy to use, light-weight JDBC framework -
jazy smith
Ranch Hand

Joined: Nov 18, 2009
Posts: 101
Hi Sebastian,

static initializers will run



Please explain what does it mean ?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
it means that all class varibles (variables marked as static) will get initialized and if your class contains the static block (check code below) this one will run too.

jazy smith
Ranch Hand

Joined: Nov 18, 2009
Posts: 101
when class loader loads the class ( the class that having main method ) then only static variables gets initialized ? you mean primitive data type variables like int, char won't get initialized ?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
no, i mean that instance variables do not get initialized.
jazy smith
Ranch Hand

Joined: Nov 18, 2009
Posts: 101
suppose I have a class

class demo
{
int x;

public static void main(String a[])
{
int y;
}

}

here, i have not initialized any of the field of the class. When this class gets loaded into memory, will it get initialized ?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
the class will get loaded but there is nothing inside of it that could need initialization. x is an instance variable, how can it be initialized within a static context. y is a local variable, it only shows up upon method invocation.

Please put code between code tags. And class names always start with a capital letter.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

jazy smith wrote:here, i have not initialized any of the field of the class. When this class gets loaded into memory, will it get initialized?

That depends on what is loading the class. Loading and initializing are two different steps, but we've already been through that in this thread. If you call Class.forName(...) to load the class, then yes, that will initialize the class.

And as you already know, initializing the class means running all of its static initializers. The class you posted doesn't have any static initializers or static variables, so initializing that particular class does nothing.
jazy smith
Ranch Hand

Joined: Nov 18, 2009
Posts: 101
hi Paul,



In the code above, when Demo gets loaded into memory, it doesn't have static fields. So this class will not get initialized ?

initialization means only executing static fields ?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
I think you have the wrong understanding of initializing.

Initializing means nothing more than executing everything that is static (on class level) before the class is first used. That's all.

Imagine this:


now, before initializing, this int variable holds 0 as it's value. In case of objects it would hold null.

Imagine in another class you try to access this constant, but the class would not have been initialized beforehand. What happens is you get a faulty value. That is why whenever the ClassLoader lays hands on a class, it's statics will run before any calls to the classes members are allowed.
jazy smith
Ranch Hand

Joined: Nov 18, 2009
Posts: 101
if there is no static field in the class the how to initialize that class ?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
There is no reason to initialize it. Again, you have the wrong understanding of initialization.

Let me try it this way:

You enter the main method of your program. From there you try to access another class. The JVM thinks 'Oh gee this class is not in the memory yet, I should do that before he can use the classes members and methods' .. So the JVM goes, calls the ClassLoader and tells him to please load the class into the cache. Once this is done the JVM inspects the class and sees 'oh wow, there are static members in this class. He might want to use these members in his call, so I better make sure all values are assigned to them before he makes his first call'.

The last step - assigning the values - is called initialization
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

jazy smith wrote:if there is no static field in the class the how to initialize that class ?

You don't seem to understand that zero is a number too. If there are zero static members to initialize, then that means that initialization does nothing. It doesn't mean that initialization wasn't done.

Consider this loop:


Run that code if you like, but you should be able to tell that it doesn't print "Something was done" but it does print "The loop is finished". So you may continue to argue that the loop wasn't executed, but the fact is that it was executed. It just didn't do anything.

Programmers ought to understand zero is just another number.
>
jazy smith
Ranch Hand

Joined: Nov 18, 2009
Posts: 101
Thanks sebastian and Paul.

I got it now. only a last question is if class has only non static fields, the only way to initialize these fields is to create instance of that class. True ?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
yes.

Because these variables exist 'one per instance' not 'one per class' ... so the only way to initialize them is to do than upon instance creation.

Besides doing that in the constructor, you can use this notation.


the upper one (in the curly braces) runs first
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: loading the class and initialization of class
 
Similar Threads
Differece between Initialization and Assignment
static Code I've never seen!
K&B Book for Scjp 6 - Chapter 5 question 8 doubt
doubt in initializing the static final variable.
Doubt in Effective Java