File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static method" Watch "static method" New topic

static method

Eureka Jana

Joined: Sep 04, 2002
Posts: 18
I know what is meant by static method or variable.But I came across one function like
// 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,
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
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).

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: static method
It's not a secret anymore!