wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes when to use static? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "when to use static?" Watch "when to use static?" New topic
Author

when to use static?

Moses Henda
Greenhorn

Joined: Nov 01, 2004
Posts: 3
Hi i wish to enquire when do we need declare the properties of a class
static.And whats the difference between normal variables and static variables?
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Normally each object has its own copy of the members defined within the class that was used to create it; these are known as instance variables. The static modifier can be used to state that a particular member is to be shared between all the objects for that class. Static members are sometimes referred to as class variables because the variable is related to the class, not to each object created from the class.

The static specifier ensures that the allocation only occurs once, regardless of the number of objects that are created. This provides the programmer with a common variable for all instances of a class.

The static specifier cannot be used with local variables, i.e. variables declared within a method. But it can be applied to methods and has the same effect, for example, the main method of a class needs to be static so the JVM can run the program without creating an instance of the class.

Hope the above helps (I also have had some fun understanding this)
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

static is a class level variable, shared by all instances of that class.
This should be a good starting point - http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
Moses Henda
Greenhorn

Joined: Nov 01, 2004
Posts: 3
thx guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when to use static?
 
Similar Threads
Class - Static variables Questions?
Static variables
A method that returns null.........
Static variable
How is a compile-time constant static vaiable causing initialization???