• Post Reply Bookmark Topic Watch Topic
  • New Topic

static code  RSS feed

 
sirisha sirisha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , waht does it mean by class loadtime.i thouhgt class load time means when we create object.

class FearFactory{
static{
System.out.println("Genesis");
}

FearFactory(){
System.out.println("Birth");
}

public static void main(String args[]){
//I'm Lazy
}
};

here it prints genesis.
static code gets executed at classloadtime.but i expected it not to print naything as no object is created in main.
can anyone help me with this.
thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sirisha sirisha:
Hi , waht does it mean by class loadtime.i thouhgt class load time means when we create object...

A class is loaded when any of its static members are accessed. This can happen without instantiation.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static initializer is going to run the first time the class definition is loaded into memory.
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Lynn:
The static initializer is going to run the first time the class definition is loaded into memory.


to add more , it will run only once for a particular class defintion.
[ April 20, 2006: Message edited by: faisal usmani ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!