• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating SingleTON without static variables

 
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
Pie
Posts: 20611
63
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
Bartender
Pie
Posts: 6128
74
  • 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
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic