Benefits of static variables: constants can be defined without taking additional memory (one for each class) constants can be accessed without an instantiation of the class Benefits of static methods: instance-independent behavior can be defined without fear of accidental interaction with an instance of the class.
Mat Not particularly. There are most certainly thread-safety concerns (benefits and disadvantages) with static methods and variables, but the basic philosophies apply the same as to instances of the classes. Example: You may not want to do public synchronized void foo() { } in a class because someone else could synchronize on that Object's monitor, and you'd be locked out.In the same manner, you would avoid static synchronized methods.
– glowcoder Apr 26 at 23:00 @Mat Also, if you were to have a public static variable, it would be obviously visible to all threads that use that class, so it should be probably final and immutable, or have synchronized access. At the very least it should be an atomic read/write. But the same philosophy applies with regular classes as well, so I didn't mention it in the post.
– glowcoder Apr 26 at 23:02.
There is an ongoing debate about the usage of static. When you make a variable or method static, they are not longer subject of inheritance, which makes them less flexible (e.g. Problems with unit tests). That said static methods are useful if they don't need an instance.
A typical example are the methods of java.lang. Math, and most people would agree that static is here fine. Another use is to have a factory method as a "starting point" in order to interact with with a library or framework, like getting an initial JNDI context or an EntityManager for JPA.
However, factories shouldn't be overused, once you have something in your hands, you shouldn't need to call factories again. A modern replacement for factory methods is dependency injection (e.g.In Spring, Guice or EJB 3. X).
Static variables are usually used for "constants" (like Math. PI). Enums are internally implemented using this technique.
Note that the old Singleton pattern is considered now potentially dangerous (e.g. Imagine that you need to introduce pooling to improve performance), and if you really want to implement a Singleton, the best way seems to be a Enum class with only one element. Other uses of static variables include things like registries or global properties, but as you can guess, this is again not very flexible and should be avoided. For performance reasons it might be fine to reuse "service objects" (I don't know of there is a well-defined name for such objects) that are expensive to create by making them static (Calendar, Random, Formatters like DateFormat, Logger), but be careful to avoid threading issues.So methods and variables should be never made static just to find a place for them.
The are in conflict with OO-principles (especially with inheritance), tend to be inflexible and hard to refactor and to test. The usage of static is fine for real, immmutable constants (however often Enums are the better choice for this), "service objects" or totally object-independet methods. They might be a solution when factories are needed (however, consider dependency injection or Service Provider Interfaces instead).
Try to avoid other usages.
While I agree with many of the points in here (+1), I feel compelled to say that static fields and methods are inherited through the extends mechanism. See examples here pastebin. Com/dr4Pq0hy – glowcoder Apr 26 at 21:36 1 They are not inherited but visible.
This is different, e.g. You can't access static methods of your base class using super, you can't have abstract static members... One of the main problems is that there is no way to express (e.g. In an interface) that a static method is present: It would be nice if all classes extending Number would be required to implement a valueOf(String) method, but this is simply not expressable. – Landei Apr 27 at 6:46.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.