File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

local variable can't be declared static

 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2373
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic