• 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

Referencing Methods

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess my C++ training is still way too embedded, because I'm having trouble with the concept of always needing an ojbect upon which to invoke a method.
Basically, I'm writing a section of a program that will read a config file and set its options. The C++ programmer in me wants to, once the file is loaded, loop through line by line, each time calling a setConfig(String value, int place) function, which would be just below the end of the function my loop is in.
Sadly, I've found, I can't just say this.setConfig(blah, blah). It wouldn't make a lot of sense to instantiate an object of the class these functions are found in...or at least so I think. Putting my setConfig function in its own class and instantiating an object of that type is the only thing that seems logical...but to me, it also seems to make for difficult-to-manage code, while sapping system resources through constant object instantiation.
What would be the best way to go about doing this -- best meaning easiest to manage and taking up the least system resources -- that you can think of?
Alex Kirk
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I guess my C++ training is still way too embedded, because I'm having trouble with the concept of always needing an ojbect upon which to invoke a method.


Actually, you don't always need an object...you always need a class. This is how you are able to call static methods without having an instance of the class, e.g.:


Sadly, I've found, I can't just say this.setConfig(blah, blah).


Have you tried just calling setConfig( blah, blah )?

Have you looked at the Properties class in java.util?
Jason
 
Alex Kirk
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I'd not just tried setConfig(blah, blah), as I'd had something akin to it cause an error for me yesterday. To my chagrin, though, it works.
I took a quick look at the API documentation of java.util.properties, and I'm not quite sure how it would work (I have a lot of trouble extracting useful information from the API). Could you point me to any examples of its usage?
Alex
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that the API is not a great resource for those who are still having fundamental questions about how to program in Java. I certainly found it very hard to use. I kept complaining "this sure isn't like MSDN".

On the other hand, once you are chugging along in Java, the API is a wonderful reference.
 
Jason Ford
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read about Properties here. If you're uncomfortable with properties, just stick with what you're doing.
I'm still getting used to the API. I'm getting familiar with exploiting classes that I find...my problem is discovering the class I'd like to use exists. My approach now is to assume that any really useful class has already been coded; all I have to do is look for it.
Jason
 
reply
    Bookmark Topic Watch Topic
  • New Topic