-2

I was looking into a project whit 5000+ classes, then I see this in each class:

private static int foo = 1;

private static void setfoo(int value)
{
     foo = value;
}

private static int getfoo()
{
     return foo;
}

so, I think why dont use a global variable and set it if anyway is static.

There is a resource use difference between global methods and variables?

Thanks you.

Marcos Eusebi
  • 617
  • 7
  • 17
  • Ummmm...I think `public` and `static` variables (or a getter that is `public` and `static`) are as close as you can get to global variables in Java. Besides, globals are usually something that you want to avoid. –  Dec 22 '12 at 16:57
  • 1
    There's no such thing as a "global method" or "global variable" in java. A class can have static methods or fields. Which is what you just posted. – Brian Roach Dec 22 '12 at 16:59
  • 1
    If these static variables are private and their accessors also are, then it doesn't make much sense to access them via getters and setters. It just adds noise, IMHO. And static mutable variables are a smell anyway. – JB Nizet Dec 22 '12 at 17:02

3 Answers3

2

Getters and Setters are useful, if you e.g want to add validation in the future (or add logging, or make access statistics or ...)

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
0

If the getter/setter exist, always use them. getFoo and setFoo effectively define a property named "Foo", which is conceptually different from your variable foo. foo is just the variable holding the current value of that property, and could be considered as belonging to the property.

Keep in mind, though, a property doesn't have to just get/set its backing variable. It could also validate the value you pass in before setting it. It could synchronize access to the variable. And/or in the future, a setter might alter the characteristics of some other thing, and the getter could return the current state of that other thing, without even needing the backing variable. By setting the variable directly, you create compatibility issues in the case where the property is/would be changed to do any of that in the future.

If, however, you know that setFoo does (and, in the near future, will still do) nothing other than set a variable named foo, then it'd actually be better to not have a getter/setter. They're not any cleaner, or more readable, and they're certainly not more efficient; a = 3; doStuffWith(a); beats setA(3); doStuffWith(getA()); on all counts. And their absence rids you of the conceptual baggage of a property where you only need a variable. Only add the getter/setter when you need (or definitely will need very soon) the special behavior a property can provide. But once you've added them, use them everywhere.

cHao
  • 84,970
  • 20
  • 145
  • 172
  • I would agree if the accessors were public, but they're private. So a change in the code would only affect the class itself. – JB Nizet Dec 22 '12 at 17:09
  • @JBNizet: Doesn't change the case where the code's no longer compatible with *itself*, and you'd have to change a hundred different `foo = 1;`s to `setFoo(1);`s before you could implement some special behavior behind `Foo`. :P The getter and setter are already there; they should be used. – cHao Dec 22 '12 at 17:11
  • So, are you suggesting that we should never access any private variable directly from inside the class where it's defined, but always using a getter/setter? I prefer having clean readable code now and refactoring it later only if needed, rather than cluttering my code now just in case something happens in the future. – JB Nizet Dec 22 '12 at 17:15
  • 2
    @JBNizet: I'm saying that *if the getter and setter exist*, yes, they should always be used rather than setting the variable directly. You now have two different says to set `foo`, which conceptually, though, is *not the same* as `Foo`. And only one of those is guaranteed to always set `Foo`; the other just sets `foo`. If there will never be special behavior attached to `Foo`, then the getter and setter should be done away with...but there should not be two accepted ways to mess with the exact same variable. – cHao Dec 22 '12 at 17:19
0

Both the field and getter and setter are private and static.. This means their intended purpose is to be used within the class only . So if you create it at some all class accessible place(globally accessible) , another public class then also you will have to keep track of the field as in every class it has to be initialized to some value before being used (in ur case its 1 i feel). Also you will have to make this code mutually exclusive to keep it correct all the time( would really make it slow if called 5000 times).. Take your call.. its all upto you..

KDjava
  • 2,355
  • 4
  • 17
  • 22