Added: 1 year ago
From: ProgramJava
Views: 4,897
Sort by time | Sort by thread (beta)

Link to this comment:

Share to:

All Comments (13)

Sign In or Sign Up now to post a comment!
  • just make the constructor private. this way you can't make an instance of the class.

    so, when you want to get the current instance, you call a function like getInstance() and you're all set.

  • @andreibranescu

    Well, though this is true there are certain things this might not protect the code from. For instance a serialized and then unserialized version could cause a conflict to where getInstance does not match the original instance, where Enum has taken care of such things. A corner case (ie: unlikely) but if one is writing a web application becomes very like as serialization happens a lot due to the application being over multiple servers.

  • Thanx for this video, it helped me alot :) keep up the good work!

  • @fpcoolw Thanks for the support I appreciate it!

  • Cool video dude .Thanx for sharing ur knowledge.one thing i would like to ask how to compile and execute a packaged source code without using the IDE. i work on windows xp and vista .

  • @bablobko well it is not as easy, you need to be able to use your command prompt. Which will mean adding an Environment Variable (if one doesn't exist) to point to your java library. If you don't have one you need to download the compiler from java.sun.com you will be redirected. Netbeans, Eclipse, IntelliJ should work on any current Operating System, just watch the videos on how to install and go from there. Have fun programming Java!

    Thanks for watching and leaving a comment.

  • one thing i would like to ask how to compile and execute a packaged source code without using the IDE. i work on windows xp and vista

  • you are already doing great job keep it up.

  • Hi, you need to synchronize the block when u create the new instance to just to avoid having multiple instances.

    public static MySingleton getInstance() { if (_instance==null) { synchronized (MySingleton.class) { _instance = new MySingleton(); } } return _instance;

    }

  • @aelgali

    I do like to avoid singletons, though its not much penalty here at all, there are still issues when this object is serialized and deserialized of having more then one singleton, or different objects etc....

    I dont mind the singleton method when coding for myself because it usually is not complex, and very simple to track.... Professionally however, one should use Enum, this is one of the things it was designed for.... Especially with threads!

  • Hi. I like your tutorias, especially wicket tutorials. Just one comment on Singletons. I think it would have been easy to make a private constructor on School instead of an enum.

    What do you think about? Regards.

  • @leonardoauer

    Yes, that would indeed limit the issues in creating a constructor, though there might be some other issues whether thread safety, or serialization. That being said, when I make an app for personal use... I likely wont have threads or serialization, so private constructors would work just fine. That being said, why not use enums? I don't use them often so I stray away, but I like most Java developers should use them even though many programmers I am guessing don't.

  • singleton sounds lonely

Loading...
Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more