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?
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?
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
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.