Jump to content
  • Advertisement
Sign in to follow this  
moth_wannaKnow

[java] Singleton pattern

This topic is 4468 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hallo! I am programming in java and I have to make sure that only one instance of a class is created. This instance should also be used by other classes. I am trying to use singleton but, it is not working.... This is how i want to get hold of my con: con c = new con.getTheOne_con(); but it's not correct because when I compile it I get that it can't be resolved to a type In my con class: private static con one_con = null; private con() { counter = counter +1; System.out.println("con created: "+ counter); } public static synchronized con getTheOne_con() { if(one_con == null) { one_con = new con(); } return one_con; } please help.....

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by moth_wannaKnow
Hallo!

I am programming in java and I have to make sure that only one instance of a class is created. This instance should also be used by other classes. I am trying to use singleton but, it is not working....

This is how i want to get hold of my con:
con c = new con.getTheOne_con();

Hint, think about what the emphisized line is TRYING to do...

Share this post


Link to post
Share on other sites
I'd say your problem is that "new", you're trying to new a return value which isn't exactly what new is for so that's why it's not working (you can only new types) - and really counterintiutive when you're using Singletons (only one instance is allowed yet you're trying to make a new one?).

So, just remove the new:
con c = con.getTheOne_con();

Share this post


Link to post
Share on other sites
Thanks

It's a little bit to late for me I can't think that clear 3- 4 oclock in the morning...but removed "new" and ofcause it's working now thanks again...

Nu ska jag sova!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!