File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes static block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static block" Watch "static block" New topic
Author

static block

venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
static blocks are executed when class gets loaded in mem, but my question is when does the class gets loaded in mem?? does that happen when we create objects???


SCJP1.5
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

a class gets loaded when the JVM starts. This means that classes are loaded even before main method is called.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
Thanks Ankit, you have told like class get loaded before main is called.
so, the class that contains the main method only will be loaded in mem, but how about the other classes in the same .java file, when will those classes be loaded?
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

The static block gets executed when class is initialized , and class is initialized when one of the following things occurred !

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

* T is a class and an instance of T is created.
* T is a class and a static method declared by T is invoked.
* A static field declared by T is assigned.
* A static field declared by T is used and the field is not a constant variable
* T is a top-level class, and an assert statement lexically nested within T is executed.

Invocation of certain reflective methods in class Class and in package java.lang.reflect also causes class or interface initialization. A class or interface will not be initialized under any other circumstance.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
The static block gets executed when class is initialized
But static variable gets executed when class is loaded in mem , that is when jvm starts.
---
are the above statements correct?? am really messed up with lot of information that you have given. so let me confirm my point before i look at those points which sagar had given... please let me know..
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by hari harann:
The static block gets executed when class is initialized


Right,

Originally posted by hari harann:

But static variable gets executed when class is loaded in mem , that is when jvm starts.


No when JVM stats, no class is initialized (except ClassLoader class), its get initialized only when any one of the above listed condition met !

Dont get confused with static block execution and with static variables intilization , they are merely the same thing !
---
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
also sagar, you have told like

'static block gets executed when class is initialized'
but i have found that
'static block gets executed when class is loaded in memory', in the following link:

http://www.coderanch.com/t/411419/java/java/static-block-static-variable
---
so, which is correct. or class getting initialized and class getting loaded are one and the same?? but i dont think so.. someone please explain
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
'static block gets executed when class is initialized'
but i have found that
'static block gets executed when class is loaded in memory',

A class gets loaded into memory only if it is about to be initialized, so the statements are saying the same thing.


Ping & DNS - my free Android networking tools app
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by Ulf Dittmer:

A class gets loaded into memory only if it is about to be initialized, so the statements are saying the same thing.


Ulf is right !, Consider loading and internalizing class as same thing !
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
based on the above discussion, the following prog should give output as 10,
since static block are executed when class gets loaded. but it gives a compiler err saying 'cannot find symbol'... so where is the problem actually???
Phil Hopgood
Ranch Hand

Joined: Jul 14, 2008
Posts: 47
Hari,

Bearing in mind that I, too, am a newbie and was a bit confused about this, try the following:



Assuming that you meant the variable xx to be static, then the static block can be used to initialise it. The static block can be used to do anything that it's legal to do at that time, i.e. anything that's not related to an instance of CDummy. Note that in my version I have declared the static variable outside of the static block ....... mind you I'm not sure why you expected the value to be 10 though.

As xx is static and therefore there is only one copy of it, the code "o.xx" can be misleading since it can lead you to think that it's part of the o object when in fact it's part of the class CDummy. So a clearer way of doing this is to always use the class name when referring to static variables.

Have a look here at this thread where I asked a load of similar questions.

Hope that helps.
Regards and happy Saturday,
Phil.

[ August 09, 2008: Message edited by: Phil Hopgood ]

[ August 09, 2008: Message edited by: Phil Hopgood ]
[ August 09, 2008: Message edited by: Phil Hopgood ]
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
Thanks Phil.. it seems you had so many similar questions which i have now.. even after reading that long discussion from the link that you gave me, i could not get the solution for my question... so my qustion is very clear.. why is the prog not getting me the output as 9 (sorry, i have mentioned as 10 in my previous post ). since static blocks gets executed when class is loaded, then my prog should run successfully as i have declared and initilized the variable in the static block....
public class CDummy
{
static
{
int xx = 9;
}
public static void main(String args[])
{
System.out.println("CDummy.");
CDummy o=new CDummy();
System.out.println("xx = " + o.xx);
}
}
please give me the solution..
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
public class CDummy
{
static
{
int xx = 9;
}
public static void main(String args[])
{
System.out.println("CDummy.");
System.out.println("xx = " + CDummy.xx);
}

}
even the above prg, i which i did not use an obj to access a static variable, did not work??
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

First thing first, Static blocks are for initializing the static fields (class variables ) . Thats what there main intention is .

Now moving towards your program ,



Your code contain a variable definition with a block scope , thats mean the scope and life of 'xx' variable is only in that block , outside the block it is dead !

If your OK with this, tell me How the compiler is able to find the non existent class variable ?
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
ok sagar.. you had cut it short with your explanations. i am ok with what you have explained..thanks
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

The main gotcha here is , a static block is like any other normal block, its just a name that is static and execute when class is initialized (or loaded , to avoid confusion ) ,

Now , we may have scenario like this , but in method,



Hope this clarifies !
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
Thanks sagar, you have taken pain to solve my queries.simply great
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static block