A friend of mine was complaining the other day to me that Java doesn't allow declaring a varaiable static within a method. He wanted to keep that variable in stack to save memory for future usage. I told him you should declare it as a static member of the class in that case. Declaring local variables as static this way would break encapsulation and cohesiveness.
Would you like to comment on this?
Ashik Uzzaman Lead Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
It sounds to me like your friend is confusing one of the meanings of "static" in C (i.e. using internal linkage to "hide" an otherwise global variable from outside that source file) with the very specific meaning of "static" in Java (a "static" class or method is not associated with any one class instance, but rather with the class as a whole).
Well he was trying to declare a primitve int variable as static so that he could use that from another method when needed. Java gives us this facility by declaring the variable as class variable (using static keyword) rather than instance variable.
Originally posted by Ashik uzzaman: Well he was trying to declare a primitve int variable as static so that he could use that from another method when needed. Java gives us this facility by declaring the variable as class variable (using static keyword) rather than instance variable.
...or as member variables without the static keyword. Since I don't know the details, I don't know which would be more appropriate. It sounds like this friend of yours needs to learn more about Object-Oriented design. Using static variables in the way he wants suggests to me that he is still stuck thinking as a procedural programmer.
I once had similar thoughts to your friend. As far as I remember, a static local variable in C(++) is similar to a member variable in Java, but can only be accessed in that one method.
Today I think that the desire to do this is a code smell - more specifically, if a member variable is only needed by one method of a class, that is a sign of low cohesion in that class. I find it to be beneficial to think about wether that method actually should be on its own class.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ashik uzzaman: He wanted to keep that variable in stack to save memory for future usage.
In addition to the above excellent advice, you might want to let him know that static variables in C aren't kept on the stack. If they were, they'd be destroyed when the function exits along with the other local variables. They simply global variables that only that method can see.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com