Results 1 to 7 of 7

Thread: One-time object

  1. #1
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default One-time object

    How to make an object, that can only be used in java-prog one time, e.g. when I try to make an second line like that:
    Java Code:
    FooObj bar = new FooObj (baz, quz, quuz)
    the compiler will start to complain. How to do that?
    P.S.: Sorry for my poor english.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Read about Singletons.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    Read about Singletons.
    Mmkay, I've done it and tried to implement some stuff, but I'M getting error msgs and I don't understand them. The Prog:
    hxxp://pastebin.ca/1708189
    and here are the error msgs:
    Java Code:
    javac "MySingletonTest.java" (in directory: /media/DATA AND SH/uni/semester_3/inf_iii/uebeungsblaetter/a_7)
    MySingletonTest.java:3: cannot find symbol
    symbol  : constructor MySingleton()
    location: class MySingleton
    	private static MySingleton _instance = new MySingleton();
    	                                       ^
    MySingletonTest.java:34: MySingleton(java.lang.String) has private access in MySingleton
    		MySingleton IAmSingleWhatsWithYou = new MySingleton ("uk");
    		                                    ^
    2 errors
    Compilation failed.
    I've used the official tutorial from sun:
    hxxp://java.sun.com/developer/technicalArticles/Programming/singletons
    what to do

  4. #4
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    public class Elvis
    {
         private static Elvis instance;
          
         private Elvis()
         {
    
         }
    
         public static Elvis getInstance()
         {
                 if(instance==null)
                    instance = new Elvis();
    
                 return instance;
         }
    }
    this is one way of implementing the Singleton pattern...
    Last edited by wtd_nielsen; 12-09-2009 at 09:28 PM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Quote Originally Posted by wtd_nielsen View Post
    this is one way of implementing the Singleton pattern...
    That way is not threadsafe.
    The more common technique is:

    Java Code:
    public class Elvis
    {
         private static Elvis instance = new Elvis();
          
         private Elvis()
         {
    
         }
    
         public static Elvis getInstance()
         {
                 return instance;
         }
    }
    This ensures no threading issues.
    Obviously in a stand alone, single thread, application it doesn't matter, but it's good to get used to using the above anyway. Makes sure you don't accidentally do the non-threadsafe one in a webapp, for example.

  6. #6
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    thats true, it depends on what you want. there is also a solution that combinds lazy initialization and thread safety..
    I forgot that this was in the Threads and Synchronization forum.. :)
    Last edited by wtd_nielsen; 12-10-2009 at 10:43 AM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    -Tolls looks up-
    Oh, so it is!

    Have to say I've never used the lazy initialisation one since I've never written a Singleton that had any other static methods, or public static attributes, than getInstance().

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  2. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 01:06 AM
  3. Time constraints on object state
    By Flipke in forum New To Java
    Replies: 4
    Last Post: 04-11-2009, 06:12 PM
  4. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  5. Replies: 1
    Last Post: 07-22-2008, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •