Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating SingleTON without static variables  RSS feed

 
Rahul Krishnan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we create SingleTon classes without static variables.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can have instance variables and only an instance as static.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you will still need one static variable, to store the singleton instance itself.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but without it how would you controll the state of you class?

Unless you have a getInstance returning a new instance every time.

He asks:
Can we create SingleTon classes without static variables.
I reduced to one static, no more. [=
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use an Enum
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check if this enum is a valid singleton ...
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the constructor.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! that makes all enums a singleton by itself?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:Thanks! that makes all enums a singleton by itself?

It's probably a question of semantics.

There will only ever be one instance each of SingletonEnum.INSTANCE1, SingletonEnum.INSTANCE2 and SingletonEnum.INSTANCE3, but I guess you could argue that there are 3 instances of SingletonEnum.
 
Stephan van Hulst
Saloon Keeper
Posts: 7803
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regardless, it's a form in instance control. I was thinking Joanne's point is that you can easily make a singleton like this:
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I was thinking Joanne's point is that you can easily make a singleton like this:

You think correctly.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, enum constants are static final variables of the enum class. Just decompile such an enum with JAD or similar decompilers.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!