1

Could someone let me know the places where we need to use private static object in java?

the places i mean is about the reasons behind in using them or the use cases where we need to use them.

Thanks, Kathir

Kathir
  • 2,733
  • 12
  • 39
  • 67

3 Answers3

2

Another common use is for the Singleton Pattern: http://en.wikipedia.org/wiki/Singleton_pattern

public class Singleton {
    private static final Singleton instance = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}
Matt
  • 1,148
  • 10
  • 15
0

One common example is for logger.

private final static Logger LOGGER = Logger.getLogger(Something.class);

Or even for constants local to the class.

Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33
0

example from JDK:

public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable
{
    static final long serialVersionUID = -5024744406713321676L;

    private transient HashMap<E,Object> map;

    // Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();
...
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275