• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Meaning of Declaring an object as private and static

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'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.
 
Mohd. Irfan Khan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic