[java] Clearing global variables after they have been used

Started by
14 comments, last by H_o_p_s 18 years, 11 months ago
I have a bunch of global variables at the start of a java file, but when I use them in a method and they get a value assigned to them, when I run that method again the values from before are still kept in them. Is there an easier way of clearing the values once the method has finished rather than listing all the variables and saying 'variable1 = 0, variable2 = 0' at the end?
Advertisement
I believe what you should be doing is making copies of the variables first. That way the original ones are always staying the same, and the variables that you change are just temporary.
BRING BACK THE BLACK (or at least something darker)
Java supports global variables? When did this happen? I'm going to assume you're talking about a member variable.

All you have to do is pass a member variable as an argument, and as long as it's a primitive, it will be passed by value, so what you do with the method's parameter won't change the value of the member variable. If it's an object you're dealing with or your method doesn't take arguments, you're going to have to make a copy like H_o_p_s said.
Quote:Original post by Kevinator
Java supports global variables? When did this happen?


[lol] Linky - Well if you did want 'global' per se... [wink]
Quote:Original post by H_o_p_s
I believe what you should be doing is making copies of the variables first. That way the original ones are always staying the same, and the variables that you change are just temporary.


Dosn't that defeat the purpose of making them global? (when I say that, I mean global to that class) The problem is that I have some variables repeated 10 times through the program each time I create a new method, and I was hoping to just make one that I can reuse.
Quote:Original post by boolean
The problem is that I have some variables repeated 10 times through the program each time I create a new method, and I was hoping to just make one that I can reuse.


Design wise, it would be better to repeat the varaibles in your functions simply because when you use global variables, bugs tend to sneak up on you. Let's say by accident you don't reset a variable or you forget to assign it, good luck tracking it down. If you use local scoped variables, that is minimized, since you must declare the variables in scope and can assign it then. I mean since you are just doing Uni java, it's not that big of a deal. Best design is not the first priority, rather having it works is.

If you still wanted to use the global variables, technically you do not even need to clear them out - as long as you assign them in the function before you use them So if you had something like this (C++ styled, sorry):

class MyClass{   int g_Temp;   void Func1()   {      for( g_Temp = 0; g_Temp < 10; g_Temp ++ )         cout << g_Temp; // System.out.printl( g_Temp ); ?   }   void Func2( int var )   {      // Where the bug's pop up ->      // g_Temp = var; // let's say you forget this      g_Temp += 5;      // Since g_Temp is global, this is legal, but is not desired   }   void Func3( int var )   {      // Now this is fine and will work as expected, the g_Temp does not need to be cleared out since it is simply reassigned the begin with      g_Temp = var;      g_Temp += 5;         }};
No offense, boolean, but I'll admit you've thrown me with what you're trying to accomplish. From this sentence
Quote:Original post by boolean
I have a bunch of global variables at the start of a java file, but when I use them in a method and they get a value assigned to them, when I run that method again the values from before are still kept in them

I would assume you want the values of the variables to be immutable, as in constant. Thus, you'd use them with a local variable so that you can modify the value of that variable without modifying the constant. That sounded retarded, so to emphasis what I mean you'd do something like the following :
public class FinalBoolean {   // these are declared with the final modifer   // you can use them to assign other variables values,   // but they cannot be set   private final int    _mod = 5;   private final String _strMod = "OMGLOLWTFBBQ!!!!";      // some random method   public void makeBoolean(int param1, String param2) {      // a local variable to perform calculations on      // or set/use with a final value      int iregister;      String strRegex;      // some precondition goes here      // assign some values      // using our constants      iregister = param1 + _mod;      strRegex = param2 + _strMod;   }}

Unless you're trying to do something different ... ?
- stormrunner
class Foo {    private int someVar = 10;    // method 1 - reset the value of the member variable manually    public void doSomething() {        someVar += 10;        doSomethingElse(someVar);        someVar = 10;    }        // method 2 - make a copy, so the value of someVar is not changed    public int bar() {        int aCopy = someVar;        aCopy *= 2;        return aCopy;    }    public void aFunc(int someVar) {        someVar -= 2;        // this is a parameter, which is a        // copy of the global -- they are two        // separate locations in memory -- it's a copy.        doSomethingElse(someVar);    }    // method 3 - pass the member variable as an argument    public static void main(String args[]) {         aFunc(someVar);           }} // end Foo


Upon exiting each of those methods, the member variable someVar remains what it was upon calling the methods.

Also, if you're resetting a bunch of member variables to 0 at the end of all your methods, then those shouldn't be member variables. Think about it. Member variables are variables that need class scope. If you're resetting their values on a method by method basis, they should be local.
Thanks for the help everyone, I know what to do now [smile]
Quote:Original post by Drew_Benton
Quote:Original post by Kevinator
Java supports global variables? When did this happen?


[lol] Linky - Well if you did want 'global' per se... [wink]


That is f*cking hideous ...

[wink]

This topic is closed to new replies.

Advertisement