This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

static code  RSS feed

 
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
 
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.
 
Ranch Hand
Posts: 2410
  • 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.
 
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!