Static variables in EJB should not be relied upon as they may break in clusters. Why?

Bozidar Dangubic

Referring article: http://servlet.java.sun.com/javaone/javaone2000/pdfs/TS-1295.pdf

Static variables are only ok if they are final. If they are not final, they will break the cluster.
What that means is that if you cluster your application server (spread it across several machines) each part of the cluster will run in its own JVM. If you declare a static variable that is not final, it can be changed.
Suppose that you are tracking something with a static integer that you are increasing at some point and lets say that you start with a value of 100. Say a method on the EJB is invoked on cluster 1 (we will have two clusters - 1 and 2) that causes value of the static variable to be increased to 101. On the subsequent call to the same EJB from the same client, a cluster 2 may be invoked to handle the request. A value of the static variable in cluster 2 is still 100 because it was not increased yet and therefore your application ceases to be consistent. Therefore, static non-final variables are strongly discouraged in EJBs.