• 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

How to put some code in one spot, rather than needing it in multiple classes and methods

 
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Needing to put some code in one spot, rather than needing to put it in every method I need an instance of it...its not what you are thinking, read on.

Here is the code I am speaking of:



Right now I have to put this in every method and every class I want to use p.SomeMethod() in. So in Class1.Method1 I have to put all that code, and then I can use p.getProperty("measurementProjectsUrl"), or whatever I may need to use.

The problem is, once I pass this project along, someone will have to go in and manually update each and every instance of this code, namely the file path in line four, since it will no longer be on my system. I am wanting to simplify this for that next person, so I need to figure out how to put that code in one spot, and then have a way to create an instance of it where I need it and be able to use the p.getProperty or any other method.

Knowing the little about java I do, I tried to just put all that code in a class by itself called ConstantClass, and then create an instance of it in another class like so - ConstantClass p = new ConstantClass();
then try to use p.getProperty, etc. To any vet I know this is laughable, and I even knew enough to know this wasn't going to work, but it is a starting point and hopefully can give you guys an idea of what I'm trying to do here.

So, any takers lol?

Would this be an instance of an instance? I'm not sure, thats why i dont know how to handle this
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put a static method into some class -- COnstantClass if you like -- which returns a properties object:



Then anywhere you need those properties, you can call

Properties p = ConstantClass.loadThemProperties();

Now, this will work, but you shouldn't have to load the properties every time you need them. The next level would be to load the properties just once, and store the Properties object, and return the same object every time the method is called.
 
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Worked perfectly in every way, thanks so much
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have also encountered such cases during my project. So just to clear my doubt, does the following line:

The next level would be to load the properties just once, and store the Properties object, and return the same object every time the method is called.



indicate the need of a singleton class or is there a better way to do the same?

- Kamlesh
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kumar Kamlesh wrote:
indicate the need of a singleton class or is there a better way to do the same?

- Kamlesh



No, not a singleton; just a static member variable to hold the Properties object, and then code to check if it's been created previously and only create it if the variable is null.
reply
    Bookmark Topic Watch Topic
  • New Topic