Static Ques?

Hi,

When the Object of static variable or the method will be created?

I heard my friend ,saying that when the class is loaded in to the memory the object of static var is created and when u compile the program the object for local and instance variable is created? Is it true?

When the class is actually loaded into the memory after compilation or

at the runtime? Is static variable can be serialized?

Thanks,

JavaCrazyLover

[467 byte] By [JavaCrazyLovera] at [2007-10-2 1:49:31]
# 1
Classes are loaded by classloaders when they are first referenced.Classes are initialized upon loading.Static members will not be serialized.
CeciNEstPasUnProgrammeura at 2007-7-15 19:30:44 > top of Java-index,Java Essentials,Java Programming...
# 2

> Static members will not be serialized.

Wouldn't make sense anyway. You have to load the class anyway, so serializing static constants is pointless, and you wouldn't want a static variable to change (remember that it's shared by all instances) simply because you deserialized some object.

CeciNEstPasUnProgrammeura at 2007-7-15 19:30:44 > top of Java-index,Java Essentials,Java Programming...
# 3

> Classes are loaded by classloaders when they are first referenced.

Can u just elaborate on this..What is the role of the "Bootstrap ClassLoader" here ? Is it a separate class loader different from class loader? Is there any difference between the loading of static & instance & local variable? As we know that static methods and variables are executed before the other methods & variables.

Thanks for ur reply!!!

JavaCrazyLovera at 2007-7-15 19:30:44 > top of Java-index,Java Essentials,Java Programming...
# 4

> Can u just elaborate on this..What is the role of

> of the "Bootstrap ClassLoader" here ?

The classloader to load all the classloaders, I guess. The classloader of the JVM - you have to start somewhere, don't you?

> Is it a

> separate class loader different from class loader?

> Is there any difference between the loading of static

> & instance & local variable?

What does "loading" mean?

> As we know that static

> methods and variables are executed before the other

> methods & variables.

Static methods, just like non-static methods, will be executed the moment they're called. Not sooner, not later. Variables are varables and cannot be executed.

CeciNEstPasUnProgrammeura at 2007-7-15 19:30:44 > top of Java-index,Java Essentials,Java Programming...
# 5

Look at this example,

class Test

{

Test(int i)

{

System.out.println("Test(" +i +")");

}

}

public class StaticTest

{

static Test t1 = new Test(1);

Test t2 = new Test(2);

static Test t3 = new Test(3);

public static void main(String[] args)

{

StaticTest Q = new StaticTest();

}

}

Output will be

test (1)

test (3)

test (2)

Where here static variables are executed before non-static. Similarly for methods as well. Where the static block will be executed once the class is actually loaded in to the memory. So that static block will executed before any static methods.

"Class is loaded in to the mem.once the object is referenced?"

Can u explain me on that please...

Thanks for ur thoughts...

JavaCrazyLovera at 2007-7-15 19:30:44 > top of Java-index,Java Essentials,Java Programming...
# 6

> Where here static variables are executed before

> re non-static.

Great. You've just proven that instance variables are only initialized when an instance is created. Surprise. And you cannot "execute" variables, as much as you cannot execute a 3. What you are talking about is initialization. And as I said, static initializations happen when the class is loaded. Non-static initializations happen after the appropriate object is created. Otherwise there would be noting to initialize.

> Similarly for methods as well. Where

> the static block will be executed once the class is

> actually loaded in to the memory. So that static

> block will executed before any static methods.

Yes. Because static methods will only only executed when you call them, if you call them.

> "Class is loaded in to the mem.once the

> e object is referenced?"

>Can u explain me on that please...

>Thanks for ur thoughts...

The class is loaded into memory when it is first used. Not sooner.

CeciNEstPasUnProgrammeura at 2007-7-15 19:30:44 > top of Java-index,Java Essentials,Java Programming...
# 7
> The class is loaded into memory when it is first used. Not sooner.Can u please eloborate on this...First used means ?Thanks..
JavaCrazyLovera at 2007-7-15 19:30:44 > top of Java-index,Java Essentials,Java Programming...
# 8
> > The class is loaded into memory when it is first> used. Not sooner.> > Can u please eloborate on this...First used> st used means ?>Thanks..first referenced elsewhere
Torajiroua at 2007-7-15 19:30:44 > top of Java-index,Java Essentials,Java Programming...