• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static variables, methods

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic