• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

How to stop class from initilizing...?

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I want to know that how to stop my class after it gets initilize..

Actually what I am doing is that I am calling my class from my another which reside inside same package.. by declaring its object.. Now once my all the instance variable inside the called class have been initilized. right?? but if I will call that class againg by declaring the another object it gets re-initilize. So If I want to stop the instatnce variable from initilizing after once it gets initilized...what should I do?

Waiting for your reply,
Regards,
Siddharth
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See when you declare a new object .. it get reinitilized because it's a different object. Check google for Singleton class this might help you.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
definitely check out the Singleton design pattern (Here's an article that Dirk wrote for our monthly journal: Question: What's a Singleton?

But with a singleton, you only can make one instance of an object of the particular class.

If you want to make multiple instance of class, but only want some certain initializationcode to run once; try putting the code in a static initializer. That way the code will only be executed once -- when the class is first loaded. (This google search may find some good details: +java +"static initializer")

Hope that helps. Good Luck!
[ April 15, 2005: Message edited by: Jessica Sant ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic