• 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

Using parameters in methods vs static variables

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question about methods that take parameters. When is it better to take parameters in a method vs. using no parameters and just using static variables in that method? I'm still doing basic programs in Java but as of right now I prefer just using static variables instead of methods with parameters.

Thanks.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple (absolutism) answer would be never use static variables when passing parameters can be used. The scope of the parameter will be smaller, which could lead to memory savings. Using static variables will lead to unsafe data escaping. and what happens when you need multiple instances of the same class? Or when you use the same static method from multiple threads? Disaster!

Sometimes static variables have their uses (usually as private). When they are final (i.e. used as 'Constants' rather than variables) then they get a bit safer. But you should never consider them a substitute for parameter passing.
 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks this clears things up for me.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic