public class javaprac1 {
private javaprac1() {
}
static private javaprac1 instance = new javaprac1();
public static javaprac1 getInstance(){
return instance;
}
}

- 2,846
- 3
- 21
- 37

- 11
- 1
-
Possible duplicate of [Thread Safe singleton class](http://stackoverflow.com/questions/16106260/thread-safe-singleton-class) – Julien Lopez Nov 18 '16 at 10:24
-
Do you mean "Will be the below code stop being (or behaving) singleton when two threads access at same time" ? – Raúl Nov 18 '16 at 11:52
5 Answers
Yes. That's a non-lazy singleton which is the simplest, and is thread-safe without any additional trickery. The only "downside" is that it's not lazily initialized, but often it seems the laziness is overemphasized. Lazy-init is preferrable when the singleton performs some potentially heavy initialization, and you want to defer it to the time it's first used.
If you need a lazily inited singleton, use the Enum singleton pattern, which is also very simple.
If you mean '..stop being singleton..' - answer is NO. But if you mean 'is this code thread safe' (which I doubt you mean) - answer is Yes.
Better / latest approach will be ujsing single value enum singleton. Other answers are really good.
Thank you @kayaman, I learned something today.
-
-
2There's no need to add a redundant `instance == null` check. The variable is eagerly initialized and is guaranteed to be visible to all threads. – Kayaman Nov 18 '16 at 11:35
-
Thank you, but I beg to differ. This > ibm.com/developerworks/library/j-dcl/ , listing 1 seems contradictory. – Raúl Nov 18 '16 at 11:41
-
1That listing 1 describes a broken lazily initialized singleton, while the code in the question describes a perfectly working eagerly initialized singleton. The link is also from 2002, when broken double checked locking was an issue (before Java 5). Pay attention. – Kayaman Nov 18 '16 at 11:44
Yes. But as to your approach (first approach), you can make your private field final.
So all calls to javaprac1.getInstance
return the same object reference, and no other javaprac1
instance will ever be created
eg.
public class javaprac1 {
private javaprac1() {
}
static private final javaprac1 instance = new javaprac1();
public static javaprac1 getInstance(){
return instance;
}
}
For second approach, make also your final
field public
and call it. You don't need to create getInstance
method.
eg.
public class javaprac1 {
private javaprac1() {
}
static public final javaprac1 instance = new javaprac1();
}
For third approach, Enum singleton - the preferred approach (see Effective Java- Item3).
eg.
public enum javaprac1 {
INSTANCE;
}

- 2,020
- 14
- 21
-
Correct, except that if the field is public then we're not really talking about *The* singleton pattern anymore. Even though it would work in the same way in many cases, it's not exactly the equivalent. – Kayaman Nov 18 '16 at 11:00
-
ya, making public the old approach but it was the correct way to implements singletons and I just describe it. – Ye Win Nov 18 '16 at 11:04
-
No, it wasn't. If you read Gamma & Al.'s *Design Patterns*, which I think we can take for the *de jure* definition of a Singleton, you can see that there is no mention of a public variable. – Kayaman Nov 18 '16 at 11:08
-
I will read this if I have a chance. thank for your point out but if you read Effective Java - second edition (see Item 3), there is mention of a public variable. – Ye Win Nov 18 '16 at 11:13
I am asking whether two thread accessing the code at the same time creates two objects. Answer is no i believe from the answers given by the people.
Thanks

- 11
- 1
If you are asking whether your implementations is thread-safe or not, I would say that it is actually thread-safe
Anyway, I use to do this way to combine it with lazy initialization:
public class javaprac1 {
private javaprac1() {}
static private javaprac1 instance = null;
public static synchronized javaprac1 getInstance(){
if(instance == null){
instance = new javaprac1();
}
return instance;
}
}

- 6,234
- 3
- 22
- 31
-
-
-
-
Due to same reason @Kayaman & Ye Win have wrote Yes. Would you please explain now how is that thread safe? – Raúl Nov 18 '16 at 11:21
-
1@Raúl All code examples currently in this question are thread-safe. The ways it's accomplished is just different. – Kayaman Nov 18 '16 at 11:37
-
@Raul So, the questions asks wether this code is thread-safe. You say YES, Kanyaman says yes, Ye Win says yes and so do I. And what's the matter with my affirmative answer and not with yours? I really can't understand your position – jlumietu Nov 18 '16 at 11:41
-
IMHO, what I think, OP intends to ask "Will be the below code stop being/behaving singleton when two threads access at same time". – Raúl Nov 18 '16 at 11:44
-
If he's asking that, then the answer would be NO. I read it as "is this code thread safe", in which case the answer is YES. – Kayaman Nov 18 '16 at 11:49
-
"Will be the below code stop breaking singleton when two threads access at same time" = "Is it thread safe", I agree with @Kayaman. I don't understand the question in any other way – jlumietu Nov 18 '16 at 11:54
-
-
@Raul, I'm pretty sure you are one of the downvoters (please tell me if I'm wrong), so according to your last edit on your own answer, which is now just the opposite you said first, It would be fair enough to at least rollback your downvote – jlumietu Nov 18 '16 at 12:02
-