-1

why used this codes in AEinsam/HabReader:

private static User instance = null; // line:25

public static User getInstance() {
if (instance == null)
    instance = new User();
return instance;
}

// line:31

can I make class without this method?

fvu
  • 32,488
  • 6
  • 61
  • 79
Amon Olimov
  • 657
  • 1
  • 8
  • 18

3 Answers3

4

This is (a poorly implemented) Singleton. If you omit this static block but do not create a public constructor then your class might be useless. If you are looking for an alternative that behaves the same way I (and most others on SO) would direct you to the Enum Pattern

Community
  • 1
  • 1
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
2

This is a singleton pattern which means that class is supposed to have only one instance existing.

dratewka
  • 2,104
  • 14
  • 15
1

The writer of that code wants to make sure there is only a single instance of the User class. This design pattern is called 'singleton'.

Yes, you can make a class without the method, but there could be multiple instances of that class.

Barry NL
  • 963
  • 1
  • 9
  • 16