| Author |
Meaning of Declaring an object as private and static
|
Mohd. Irfan Khan
Greenhorn
Joined: Jul 18, 2007
Posts: 18
|
|
Hi, I would like to know what is the meaning of declaring an object as private static. What is the advantage of doing so. e.g. public class Irfan { private Irfan () {} private static Irfan irfan = new Irfan(); } This strategy is followed in Singleton Design pattern...
|
Thanks,<br />Mohd.Irfan Khan<br />SCJP1.4
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
'private' means that the member is only visible from within the class itself and not from other code outside the class. For more info, see The Java Tutorial - Controlling Access to Members of a Class. 'static' means that there is only one instance of the variable per class, instead of one instance per object of the class. For more info, see The Java Tutorial - Understanding Instance and Class Members. Now you can combine these concepts, so you have a variable that's only visible from inside the class itself and of which there's only one instance, shared by all objects of the class.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Mohd. Irfan Khan
Greenhorn
Joined: Jul 18, 2007
Posts: 18
|
|
Thank you for your response. I was confused if one instance is shared by all the objects... Thank you very for for the very lucid explation....
|
 |
 |
|
|
subject: Meaning of Declaring an object as private and static
|
|
|