Solved

Method static variables

Posted on 2000-04-20
7
217 Views
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?
0
Comment
Question by:Sasha_Mapa
  • 3
  • 2
  • 2
7 Comments
 
LVL 3

Expert Comment

by:Laminamia063099
Comment Utility
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 :)
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 5 total points
Comment Utility
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 :)
0
 
LVL 7

Author Comment

by:Sasha_Mapa
Comment Utility
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");
imageTracker.addImage(myImage,46);

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 too...no?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
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)
0
 
LVL 7

Author Comment

by:Sasha_Mapa
Comment Utility
LOL, I see what you mean now...
What's a Singleton? A static object shared between all applications?
0
 
LVL 3

Expert Comment

by:Laminamia063099
Comment Utility
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.

Laminamia
0
 
LVL 7

Author Comment

by:Sasha_Mapa
Comment Utility
Cool! I'll see whether it fits somewhere in my app. Thanks a lot guys.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
endX challenge 2 48
best (free) software to access postgres db (java) 1 17
HashMap Vs TreeMap 12 47
@SBGen Method 3 25
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 This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now