views:

355

answers:

6

Merely out of interrest: do you think Java would have been a better language if static variables would have been excluded, effectively replacing Singletons with singletons? Definition here.

If you think so: could you elaborate on what motivation there might have been for including it in the language?

+4  A: 

do you think Java would have been a better language if static variables would have been > excluded, effectively replacing Singletons with singletons?

No, sometimes you really want to have variables or constants that are common to all objects. Of course static is not necessary evil.

Could you elaborate on what motivation there might have been for including it in the language?

static is a shorthand for objects or methods that are not necessary bounded to data, it models behaviour without instance data.

dfa
+2  A: 

Despite despising singletons and anything static nonfinal, I do occasionally use the following idiom to generate unique IDs for objects.

private static long idCounter;
private synchronized static long getID() { return idCounter++; }
private final long id = getID();

So that's one argument for having static nonfinals.

(Or, as Tom Hawtin points out, the slightly better

private static final AtomicLong idCounter = new AtomicLong();
private final long id = idCounter.getAndIncrement();

)

Zarkonnen
AtomicInteger would be slightly better there.
Tom Hawtin - tackline
@Tom Hawtin: True. Have amended code to use AtomicLong, though that's Java 5+ only.
Zarkonnen
A: 

I agree with DFA. Sometimes Static makes sense.

Also it is a language feature someone who despises it can choose not to use it. I find statics and Singletons useful.

Geek
+7  A: 

The same article that you cite has the following statement:

The other kind of Singletons, which are semi-acceptable are those which don't effect the execution of your code, They have no "side effects"

... and then the article explains about logging. Another typical example is Printing. So that are arguments for Singletons even in the article that calls for "let's-get-rid-of-all-singletons".

The argument the author provides is interesting. He states that having global state is the real problem with Singletons, and as long as there is a one-way communication between your program and the Singleton you are in the clear.

And definitively Java would be a worse language if it had no static variables, as it is a feature that is required in several use cases. I mean, you could program in a procedural way in Java if you really wanted... and that is not evil per-se.

The abuse of static variables and Singletons doesn't necessarily mean that we need to get rid of them.

Mario Ortegón
Then what would be the best way to get rid of the abuse? Would you say that within a corporation, leads should be assigned to review a lot of code and be evangelic about why not to Singletons? Aren't language limitations easier in the long run, compared to just another risky pattern in the check-list?
Jonas Byström
There is always a compromise between flexibility vs ease of use. In this case, static variables are very useful for many cases. Removing them would cripple the language.
Mario Ortegón
+2  A: 

Clearly static constants are useful. To make an object constant in Java you define an immutable type. As it currently stands Java is a bit lacking in constant types. For instance, it is very common to see a static field with a (non-empty) array but (hopefully_ treated as a constant.

There are a relatively few cases where mutable statics make sense. Some caches and adding external fields to existing objects, for instance. Analogously, even the constant String has a mutable field.

So entirely getting rid of statics is a bit difficult for general purpose languages. Having siad that I believe Fan and Newspeak do that.

However, the common mutable static, whether dressed as a Singleton or not, is definitely something that could go. APIs would of course be different. It would make my job a lot easier. The SecurityManager mechanism would be redundant, for example.

Tom Hawtin - tackline
+1  A: 

I think java would be a prettier language without static, but I don't think it follows that the Singleton anti-pattern would be cured by removing the keyword.

A singleton is evil because it 'knows' something about itself that it shouldn't, namely that it is the only instance of its type in the whole system. Any time this sort of information is encapsulated in a class you have a defacto Singleton.

As to why it's in Java, I would speculate that, like a whole range of things, it is a compromise between OO purity and practicality.

CurtainDog