Results 1 to 6 of 6
- 09-10-2012, 11:32 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Question on Singleton + Private Inner Classes
Hi everyone,
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.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; } }
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.
- 09-11-2012, 08:16 AM #2
Member
- Join Date
- Jul 2012
- Posts
- 9
- Rep Power
- 0
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.
- 09-11-2012, 08:26 AM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Question on Singleton + Private Inner Classes
Thank you.
I guess I understand it.
- 09-11-2012, 10:06 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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:
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.Java Code:public class Stage { private static final Stage instance; private Stage() { System.out.println("Stage created"); } public static Stage getInstance() { return instance; } }Please do not ask for code as refusal often offends.
- 09-11-2012, 10:12 AM #5
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Question on Singleton + Private Inner Classes
Thanks, it was confusing for me as well, it is from the book Spring in Action.
- 09-11-2012, 10:45 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
Similar Threads
-
Difference between public/private/protected inner classes?
By castiel in forum New To JavaReplies: 1Last Post: 02-10-2011, 04:43 AM -
OOP Question re. private variables and extending classes
By ImplicitCharm in forum New To JavaReplies: 7Last Post: 07-28-2009, 03:46 PM -
How to access private data types from public classes?
By kevzspeare in forum New To JavaReplies: 3Last Post: 03-07-2009, 04:19 AM -
[SOLVED] Hacking Singleton- Multiple instance of a Singleton class
By piyu.sha in forum New To JavaReplies: 2Last Post: 10-06-2008, 09:06 PM -
Private Classes Clarification
By justlearning in forum New To JavaReplies: 1Last Post: 05-06-2008, 10:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks