This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes When and when not to use static variables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When and when not to use static variables?" Watch "When and when not to use static variables?" New topic
Author

When and when not to use static variables?

Nikolas Bernadel
Greenhorn

Joined: Aug 09, 2012
Posts: 9

Let's say I plan on instantiating one instance of a Car object. Since there's only ever going to be one instance of Car, does it matter if the variables within it are static?
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2271
    
  28

Any member variables in singleton objects can cause problems, whether they are static or not. If your singleton Car object is being called by multiple threads, and any of those variables store information that is relevant to one thread, then you will run into concurrency issues
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Yes, it does matter. You should, a a general rule, avoid things static unless you have a good reason to make them static. If the fields belong to the object, they belong to the object, and that means not static.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When and when not to use static variables?
 
Similar Threads
Doubt in declaration..
object hierarchy problem
Instance Variables & Class Variables
Variables in Java
Ensuring Methods Run ONCE