• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static method

 
Eureka Jana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know what is meant by static method or variable.But I came across one function like
static
{
// function body
}
But in one book I read that this will be called once when the class gets loaded.What is meant by "class gets loaded".Its during the object creation time or what.Does this gets called every time when object is created or only once(global).
I would be thankful if anybody could explain this.
Thanx in advance,
Jawahar.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create the first object instance defined by a class, the class loader will load the class.
That's when the static{} blocks get called. They are called once only. They can be used to initialize static variables. If you define variables inside the static block{}, then they will be taken as local to the static{} block and will go out of scope when the static{} block exits.
Member initialization blocks {} (no static) are executed for every object that is created.
-Barry
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jawahar M,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
-------------------
static initilizer blocks are loaded only the first time that a class is loaded. A class is loaded when it is used. This might be when an instance of the class is created or when a static class variable or method is used (so, an instance doesn't have to be created in order to load a class).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic