aspose file tools*
The moose likes Beginning Java and the fly likes Static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static" Watch "Static" New topic
Author

Static

Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
What is a purpose of static fields and methods in Java?
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

A static field or member is maintained once in memory and used by all instances of that class. One way to imagine using this is to think of an instance counter. You could increment it every time an object gets made, and each object would then "know" how many total instances had been created.
A static method can be invoked without ever making an instance of the class that contains it. Best straightforward example: all the methods in the Math class. You don't really need a Math object for anything, so why create one just to make use of its functions ? (You can't make Math objects, anyway, but that's because it's final class).
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Stanley Tan
Ranch Hand

Joined: May 17, 2001
Posts: 243
For example, imagine you have 20 people with different hair colors. If the hair color were static and one person changed his hair color to blue, then you would have 20 people with blue hair .
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Originally posted by Michael Ernest:
... (You can't make Math objects, anyway, but that's because it's final class).

The reason you cannot instantiate the Math class is becuase it has a private constructor (private Math() {}), not because the class is final. When a class is made final it is saying it cannot be extended which has nothing to do with it being instantiated.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static