0

Is it ok to declare multiple instance variables (Has-A relationships with other classes) in a singleton class?

class Singleton {

private static Singleton obj;
Student student;
School school;

private Singleton() {}

public static Singleton getInstance() {
    if (obj==null)
        obj = new Singleton();
    return obj;
 }
}
Tian Yang
  • 11
  • 2
  • 1
    What would be the problem ? You can't use Singleton pattern only with no attribut class, that would no real usage – azro Nov 02 '22 at 21:23
  • Note that this method of creating the singleton instance is not thread safe. There are many discussions of this issue on this site and others. Here’s one good one: https://stackoverflow.com/q/2912281/29470 – Tim Moore Nov 02 '22 at 21:33
  • What do you mean by OK? Do you mean valid, runnable code? Or something else? – TylerH Nov 03 '22 at 17:46

0 Answers0