This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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 ]
 
money grubbing section goes here:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!