• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using parameters in methods vs static variables  RSS feed

 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks this clears things up for me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!