• Post Reply Bookmark Topic Watch Topic
  • New Topic

Some examples of Singleton classes from Java API  RSS feed

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

This was the example from me in a training session. I was the trainer and explaining singleton class in multi-threaded environment to batch of 20 new joiners and someone ask me- is there some readily available singleton classes in java - api? And i was like..hmm and bla bla?
Can someone help me..
 
Saloon Keeper
Posts: 8994
168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't quickly think of one off the top of my head.

I hope you explained to your students that mutable state that is globally accessible is a really bad idea, and not to be emulated.
 
Ranch Hand
Posts: 86
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think of a Runtime class in the java.lang package.
 
Marshal
Posts: 59438
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of enums. Since all their instances are intentionally distinct, that is similar to Singletons.
System class. No, that isn't a singleton because it always has exactly 0 instances.
 
shivanyy gupta
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There are lots of enums. Since all their instances are intentionally distinct, that is similar to Singletons.
System class. No, that isn't a singleton because it always has exactly 0 instances.


As per my understanding, Enums are  a way to create Singleton classes, not itself it is Singleton.
 
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The logging classes (log4j, etc) are Singleton.

The database connection classes are singleton.

Thread pooling classes are singleton.
 
Marshal
Posts: 5804
401
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ravi Desigan wrote:The logging classes (log4j, etc) are Singleton.


Since when they started bundling it with Java API?
 
Ravi Desigan
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see! My bad.
 
Stephan van Hulst
Saloon Keeper
Posts: 8994
168
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even then, they're not singletons (but they are "instance limited", which is basically the same pattern).
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!