Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block

 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a class gets loaded when the JVM starts. This means that classes are loaded even before main method is called.
 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'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.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok sagar.. you had cut it short with your explanations. i am ok with what you have explained..thanks
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sagar, you have taken pain to solve my queries.simply great
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic