They reduce reuse in much the same way as global variables: when a method’s calculations depend on a state that is external to the method but not passed as parameters (for example, class fields), your method is less commonly used because it is closely related to the state of the object / class in which it is located (or, even worse, in another class).
Edit : Ok, here is an example to make it more understandable. I used ThreadLocal
just for the sake of the question, but it applies to global variables in general. Suppose I want to calculate the sum of the first N integers in parallel across multiple threads. We know that the best way to do this is to calculate local amounts for each thread, and they sum them up at the end. For some reason, we decided that the call
method for each Task
would use the ThreadLocal sum
variable, which is defined in another class as a global (static) variable:
class Foo { public static ThreadLocal<Long> localSum = new ThreadLocal<Long>() { public Long initialValue() { return new Long(0); } }; } class Task implements Callable<Long> { private int start = 0; private int end = 0; public Task(int start, int end) { this.start = start; this.end = end; } public Long call() { for(int i = start; i < end; i++) { Foo.localSum.set(Foo.localSum.get() + i); } return Foo.localSum.get(); } }
The code works correctly and gives us the expected value of the global sum, but we notice that the Task
class and its call
method are now strictly related to the Foo
class. If I want to reuse the Task
class in another project, I must also move the Foo
class, otherwise the code will not compile.
Although this simple example is complex, you can see the dangers of “hidden” global variables. It also affects readability, as someone else reading the code will also have to look for the Foo
class and see what the definition of Foo.localSum
. You should keep your classes as autonomous as possible.
Tudor source share