This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Static variables Performance Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static variables Performance Issue " Watch "Static variables Performance Issue " New topic

Static variables Performance Issue

akmal jah
Ranch Hand

Joined: Feb 18, 2002
Posts: 31
I have this class A and it has a boolean variable isValid. I am using this as
if(A.isValid) in most of the classes. but in classes where I use this several times ( more than 50 times ), I am making a local copy of it like below, like in
class B : localIsValid = A.isValid;
what is a best solution. making a local copy in B or using directly from A . details below..
FYI.. I am writing only relevant part of my class
public class A {
public static boolean isValid = false;
public class B {

// Type 1 implementation
public localIsValid = A.isValid; //1
// do nothing
// Type 2
// do nothing
what I did was , made small app out of this and did some time testing but it doesnt tell much.
What I want is some kind of thoery behind this and how it works internally and how they differ internally. ( and what type ( 1 / 2 ) is a good implementation )
apreciate it . thanks
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I think you're trying to optimize prematurely.
If your static variable can change, you'll have to keep the two copies in sync. If it can't change, you should declare it to be final.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
akmal jah
Ranch Hand

Joined: Feb 18, 2002
Posts: 31
well, once loaded from the property file, the variable value does not change , so I dont need to keep that particular variable in sync, can you please tell me which implementation is better and why , will it help making that variable final optimize my code ? thanks for your help
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Why do you think you need to performance-optimize it?
In most projects, maintenance is a much more critical issue than performance. And even if performance becomes an issue, you are very likely to optimize the wrong thing until you searched the real bottlenecks using a profiler.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Creating a copy of it just adds extra overhead (and confusion) and no performance gain.

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: Static variables Performance Issue
jQuery in Action, 3rd edition