Method static variables

Posted on 2000-04-20
Last Modified: 2013-11-23
Why aren't there static variables in methods in Java like there are in C++? Is that against OO design?
Question by:Sasha_Mapa
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2

Expert Comment

ID: 2734774
C++ static variables are against good design in general.  In C++ they are used to track changes or values from one method call to the next.  This is bad design because the same call to the method with the same parameters and same class variable values can have different results because a static method variable has different values.

In good program design, the same call to a class method with the same parameters and the same instance values should always produce the same result.  C++ static method variables break this design constraint and code becomes unpredictable.

Laminamia :)
LVL 16

Accepted Solution

heyhey_ earned 5 total points
ID: 2734823
in C (forget C++) you have functions & static variables to keep some state between function calls.

in Java you have Objects with methods and fields which keep the object state between method calls :)

Author Comment

ID: 2734907
Ok, I see.
Here is a simplification of the case when the need for a method static variable arised:
I wanted to get lots of images, add them to a media tracker and then wait for them to load. I didn't want to repeat the same code of

myImage46 = getImage(getCodeBase(),"imagename.gif");

many times so I wrote a method that would take the name of the image and the MediaTracker as arguments and would return the Image having added it to the MediaTracker. The problem is that I don't know the number of the image from within that method, and a static variable (a static variable in that method) which would increase by 1 every time the method would be called seemed the right way to go here. Not being able to use that I had to add a private instance variable to my applet. So I ended up adding an instance variable which did not represent any actual state of my applet. That sounds like bad OO design
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 16

Expert Comment

ID: 2734950
if method that loads images is part of your applet, than the information about how many images are loaded is part of that applet too :)

if you put this method in some specila ImageManager object (which can be implemented as Singleton), this field will be member of that object.

there is no such thing as 'method with state' - only object have state.

(in C you have functions with state)

Author Comment

ID: 2735030
LOL, I see what you mean now...
What's a Singleton? A static object shared between all applications?

Expert Comment

ID: 2735266
A Singleton object only is instantiated once.  It does this by controlling it's creation.  This is important because anyone who has a handle to the singleton object has a handle to the same object.  You have to make the constructor private/protected and provide a method that returns a handle to the instance:

class Singleton {
  protected static handleToInstance = null;
  protected Singleton(){}
  public getInstance (){
     if (handleToInstance == null)
          handleToInstance = new Singleton();
     return handleToInstance;

It's great when used in appropriate areas, like heyhey's suggestion.


Author Comment

ID: 2735287
Cool! I'll see whether it fits somewhere in my app. Thanks a lot guys.

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Certificat to iSeries KeyStore 1 30
Netbeans and org.apache.commons.lang3 issue 3 53
Java pass by reference 3 74
Read CLOB data from Oracle using JAVA 3 40
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question