Results 1 to 6 of 6
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Question on Singleton + Private Inner Classes

    Hi everyone,

    Java Code:
    public class Stage {
    	
    	private Stage(){
    		System.out.println("Stage crated!");		
    	}
    	
    	private static class StageSingletonHolder {
    		static Stage instance = new Stage();
    	}
    	
    	public static Stage getInstance(){
    		return StageSingletonHolder.instance;
    	}
    
    }
    I saw this example in a book and with a simple test I can see that no matter how many times I call the getInstance method, I only see one "Stage created!" in the console. So this is definitely a Singleton pattern, I can see that.

    What I do not understand is how this works.

    I can see that StageSingletonHolder is an "inner-class".

    But what does it mean to be a static class? Is it like a static field that is shared by all the instances of the class?

    So when I say Stage.getInstance -> static Stage instance = new Stage(); returns me a new Stage.
    But how does it reference to the same Stage instance when I run Stage stage2 = Stage.getInstance? Why doesn't it give me a new Stage object?

    Thank you.

  2. #2
    java.java is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Question on Singleton + Private Inner Classes

    StageSingletonHolder is a static inner class, in java if you want to make a class static it should always be a inner class , the top class can never be static.
    Within this method the Stage object is instantiated with a static keyword, which means that at any point of time only one object of type Stage() will be available in Heap memory and every time this method is called the same object reference is returned and no new object is created. If you remove the static keyword you can create as many objects as you can. Hope this clears your doubt.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on Singleton + Private Inner Classes

    Thank you.
    I guess I understand it.

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

    Default Re: Question on Singleton + Private Inner Classes

    Just as a pointer, I have never seen a situation where you actually need to write your singleton like that.
    The only reason to use this is if there are more static methods in the singleton class and getInstance is normally the only static method in the singleton class, thus rendering the inner class entirely unecessary.

    All you normally need is:
    Java Code:
    public class Stage {
        private static final Stage instance;
    
        private Stage() {
            System.out.println("Stage created");
        }
    
        public static Stage getInstance() {
            return instance;
        }
    }
    This will create the instance when the class is loaded, normally on the first access of the class, which will be the first call to getInstance.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on Singleton + Private Inner Classes

    Thanks, it was confusing for me as well, it is from the book Spring in Action.

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

    Default Re: Question on Singleton + Private Inner Classes

    It was a fad for a while, until it was pointed out how pointless it was.

    If you had another static method on the code then it may make sense, since it means the singleton would not get created until getInstance was called, but I've not seen any singletons with more than getInstance as a static method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 02-10-2011, 04:43 AM
  2. OOP Question re. private variables and extending classes
    By ImplicitCharm in forum New To Java
    Replies: 7
    Last Post: 07-28-2009, 03:46 PM
  3. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 04:19 AM
  4. Replies: 2
    Last Post: 10-06-2008, 09:06 PM
  5. Private Classes Clarification
    By justlearning in forum New To Java
    Replies: 1
    Last Post: 05-06-2008, 10:51 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
  •