The moose likes Java in General and the fly likes Meaning of Declaring an object as private and static Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Meaning of Declaring an object as private and static" Watch "Meaning of Declaring an object as private and static" New topic
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
    
    3

'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....
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Meaning of Declaring an object as private and static
 
Similar Threads
Instantiate a inner class object
How to access private static variables outside the class?
effect of private static final objects in java?
why top most class cannot be static?
private class MyClass?