• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Singleton using Enum

 
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any one have code of creating singleton using enum?
my below code is not working


 
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For common questions like these you should try searching the forums first.

https://coderanch.com/t/642527/java/java/Enum-Singleton


 
Yashwantrao Raut
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen that post but not able to create singleton using it.
anyone know to create it.my code is not working .i will post their.
 
Greenhorn
Posts: 22
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please, try this:

public class A{
public static final A singlA= new A();
}
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yashwantrao Raut wrote:I have seen that post but not able to create singleton using it.
anyone know to create it.my code is not working .i will post their.



1. Which code is not working? Please post the code if its not same as the one posted earlier.
2. What is the error you are getting?

From the post linked by Charles https://coderanch.com/t/642527/java/java/Enum-Singleton it shows clearly how to create singleton using enum.

Update:

The code posted by you in the first post doesn't work because it is not a valid Enum syntax and not a valid way to use Enum. You can learn about Enums here
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yashwantrao Raut wrote:I have seen that post but not able to create singleton using it.
anyone know to create it.my code is not working .i will post their.



Well, how to create it is exactly as in Jesper's post in that thread. You then refer to it as Example.INSTANCE. There's no need to "create" it, because it's instantiated for you as necessary.
 
Yashwantrao Raut
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, my syntax of creating singleton using enum was incorrect.
below code is working now.
Thanks a lot.

 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yashwantrao, I added code tags to your post so your code will appear as you formatted it (with its indentations). Click on UseCodeTags to learn how that's done.
 
Yashwantrao Raut
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stevens for doing this, I will use it from next.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not a singleton.
You can create another SingletonA object quite easily.

With an enum singleton the code should all be in the enum.
There's no need for a SingletonA class at all.
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic