• 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

regarding static instance variable

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

what is use of creating static instance of a class, say

class MyCalss{

static MyClass myClass = new MyClass();

}
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

Creating a Static Referance of a Class used in Singleton.In singleton you The class constructor will be of private.When constructor is private you cant directly create a instance you need to have a static method to ctreate a instance instance variables cant be used in static methods so we use static instance varible to create the class.

I think you got it
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Prashant,

I would say the main (maybe only reason) is to implement the Singleton Pattern: see here for more details.

Basically you want one and only one instance of a particular class and any client code to that class must use that single instance.

I used to use Singletons to create some kind of managers (ThemeManagers, PropertyManager etc.) because I never liked a bunch of static methods in one helper/manager class.

Here is a very simple example of a Singleton:



The client code would never do new PropertyManager() but would simply call the manager with something like:



I hope this helps!

Giovanni
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think we need to check whether the instance is null then only
we have to create a new static instance is that right for
a singleton??
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Rajeev,

it depends on what your constructor does: if the constructor opens a file or worse connects to a DB or does anything that might go wrong, then yes, you have to check if the INSTANCE is null or not (and catch any exception) and if it is you should fall back to a default (sometimes dummy) alternative.

If you are not doing anything critical then I always felt comfortable skipping that check.

In my experience, with a PropertyManager Singleton, I usually had to read te properties from a file, but if the file was not there, I never returned null but instead I logged the issue and used a default PropertyManager with default values.

I believe this brings more robust code and it is a neater design.

Just my 2 cents!

Giovanni
 
It's never done THAT before. Explain it to me tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic