It's not a secret anymore!*
The moose likes Beginning Java and the fly likes static variables, methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static variables, methods" Watch "static variables, methods" New topic
Author

static variables, methods

Mike Polinsky
Greenhorn

Joined: Jun 10, 2004
Posts: 6
I've been reading about static variables and method, but still feel confused on when and why to use them.
Can someone please refer to a link where I can once and for all get this concept settled?

thanks!
mike
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
My first suggestion would be to do a search of this forum, and the intermediate forum, as this topic has been well-covered.

Also, you can peruse a decent article here.
Mike Polinsky
Greenhorn

Joined: Jun 10, 2004
Posts: 6
thanks!
So, the basic concept behind static is that variables and methods exist outside of Class instance? They do not depend on other methods within a Class?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
So, the basic concept behind static is that variables and methods exist outside of Class instance?

Just to be clear, static class members belong to the class and not to instances of the class. Also, there's only one of any particular class while a Java program is running (ignoring multiple class loader issues), while there might be lots of instances of that class.

They do not depend on other methods within a Class?

This doesn't seem like a correct understanding of something, but I'm not sure what you mean by this. So, what do you mean?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
OK, after reading the linked Baldwin article above, I think I understand what you were getting at concerning whether static methods should be "independent". Following is an exerpt from that article, explaining static methods.

The method should probably also be self-contained. By this I mean that all information that the method needs to do its job should either come from incoming parameters or from final static member variables (constants). The method probably should not depend on the values stored in non-final static member variables, which are subject to change over time


The author is stating (without supporting arguments, unfortanately) that a static method shouldn't depend on the state of the class in which it is defined.

For the most part, this is a practice I see followed. Static methods are typically used as utility methods that are independent of the state of the system in which they are running. However, I also see the occasional use of a static member to do something like count instances of a class (in mostly contrived examples for educational purposed).

In general, going the object-oriented route and defining objects to represent the state and behaviors of a system is a good idea. For situations where only a single entity should track some state or provide some behavior, following the Singleton Pattern is a recommended practice. For the occasional situation where a simple utility function is need to perform some calculation, a static method probably isn't the worst thing in the world, but use them sparingly. There are good reasons object-oriented programmers don't program function libraries as you'll find in c or php. That topic wanders deeper into the whole object-oriented principles and thinking realm. I'd recommend moseying on over to the OO, Patterns, UML and Refactoring forum for such conversation.
Angela Evans
Greenhorn

Joined: Jun 17, 2004
Posts: 3
I thought static methods and variables belong to the class and instance variables are variables that belong to an object or reference to an object and then the local variables are variables declared within a method. I think Java Head First has an excellent explanation of this.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static variables, methods