(I'm using String as an example, but it could be replaced for Object > MB's of memory)
do this quite a lot:
private static String mTempString = "";
private static void SomeMethod()
{
mTempString = "Whatever Result";
}
Now my question is, if I was to write it like so:
private static void SomeMethod()
{
String mTempString = "Whatever Result";
}
and use it in a loop (that is executing hundreds of times a second as an example) would Java know how to manage the memory as example one? Would be memory be of the same efficiently. (Sorry I can't test this myself at the moment)
Which is more memory efficient (disregarding the fact that they are small variables)
--edit--- Found an excellent article here that explains it http://www.cs.berkeley.edu/~jrs/4/lec/08
This is excellent practice. By creating variables inside loops, you ensure their scope is restricted to inside the loop. It cannot be referenced nor called outside of the loop.
It is better to declare the variable in the loop as its validity will be only up to that block and the code can stand alone for that section. So, to tell there is no difference in anything if either of the approaches are used.
It's not a problem to define a variable within a loop. In fact, it's good practice, since identifiers should be confined to the smallest possible scope. What's bad is to assign a variable within a loop if you could just as well assign it once before the loop runs.
The recommended practice is to put the declaration as close as possible to the first place where the variable is used. This also minimizes the scope. From Steve McConnell's "Code Complete" book: Ideally, declare and define each variable close to where it's first used.
Keep the scope of your variables as narrow as possible.
This is important for several reasons:
Readability. If you use that variable in three different places with four different values (go figure), you're going to have a hard time discerning what purpose that variable is supposed to serve.
Bugs. You reduce the amount of errors that could crop up in your application if you keep a single variable to a single, well-specified scope. Suppose you had that String, and you expected it to be some value in two methods, but it was something completely different.
Intent of purpose. I mentioned this a bit in the readability section, but if you have a static
variable that keeps getting redefined, its intended purpose has become unclear. Typically, static methods and variables can be used independent of the state of the object, so if the state of the object affects the value of the static variable, the intent has become confusing.
I wouldn't worry much about memory efficiency at this point (unless you had gobs of strings, but then I'd say you have two things to worry about).*
*: First rule of optimization: Don't do it.
Second rule of optimization (experts only!): Don't do it yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With