File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes local variable can't be declared static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "local variable can Watch "local variable can New topic
Author

local variable can't be declared static

Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

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
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
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).

IMHO .. PSM
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

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.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Layne


Java API Documentation
The Java Tutorial
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: local variable can't be declared static