-2

What's better to use in Java: a singleton or a class with all static members? Or does it not matter?

For example, I want to access an X class from different classes, but the X class has to contain similar values for all classes which use it.

hopper
  • 13,060
  • 7
  • 49
  • 53
dooplaye
  • 999
  • 13
  • 28
  • Java is an object oriented programming language. – Sotirios Delimanolis Oct 21 '13 at 15:50
  • No, your requirements are not clear at all. Better in what sense? For what purpose? – PM 77-1 Oct 21 '13 at 15:52
  • You put a question with incomplete requirements. You need to get more theoretical know-ledges to decide if singleton or static methods are better. It is better to study pattern designs to get. None of answers will be good for your question enough. – Martin Strejc Oct 21 '13 at 16:13

5 Answers5

2

There are some cases, where static classes makes sense than Singleton. You can see the example of java.lang.Math. This is not singleton -. its just providing us a bunch of static methods.

If your class is not maintaining any state, and just providing global access to some methods, then you should go with using static class, as static methods are faster than Singleton, because of static binding during compile time. Remember you cannot use polymorphism with static methods as static methods cannot be overridden.

If you want to maintain the state of the object, you have to go with the singleton instead of static methods.When you need a class with full OO capability , chose Singleton, while if you just need to store bunch of static methods together, than use static class.

You can have a detailed description here Read more: http://javarevisited.blogspot.com/2013/03/difference-between-singleton-pattern-vs-static-class-java.html#ixzz2iNE3rW4i

shikjohari
  • 2,278
  • 11
  • 23
1

For me: Singleton is an anti-pattern and should only be used if there is a strong reason, also a class only holing public static variables is not acceptable in my opinion, this sound not realy object orientated to me. You could use Dependency Injection, the benefits are testability and you can avoid the doublecheck on creating a singleton (if you don't use an enum).

this would look like:

public class SharedObject{
// content
}


public class Worker{
private final SharedObject sharedObject;
public Worker(SharedObject sharedObject){
this.sharedObject = sharedObject;
}
}

With this way you also see, where the objects come from, you can easyly mock the shared object using Mocktio. It forces you to structure your code for easy testing, meanwhile it will go in a more modular direction.

Community
  • 1
  • 1
MemLeak
  • 4,456
  • 4
  • 45
  • 84
1

I prefer static methods for classes which are stateless, just like Math or System class, and singleton as vice versa->for statefull classes, like FacesContext.

0

An all-static class is often used for shared utility methods that are grouped logically together but do not share state (fields) between them.

A singleton is better if:

  1. You might want different objects with different behaviors to play that role.
  2. The object needs to implement an interface (e.g. a shared ActionListener)

(Another way of saying this: If you need polymorphism, your singleton must be an object and not a class)

Russell Zahniser
  • 16,188
  • 39
  • 30
0

It is preferable not-heritable and non-instantiable class with static methods. e.g.:

public final class Constants {

    private Constants() {
        // non-public constructor
    }

    public static final Pattern ID_PATTERN = Pattern.compile("^\\d{4,10}$");

    public static final Locale DEFAULT_LOCALE = new Locale("en", "US");

    ...

}

Use singleton only if you want to maintain some state, similar to the application scope. However, must be two classes if you want a clean design.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148