Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

local variable can't be declared static  RSS feed

 
Ranch Hand
Posts: 2373
Java MySQL Database Spring
  • 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?
 
Ranch Hand
Posts: 236
  • 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
Java MySQL Database Spring
  • 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.
 
Ranch Hand
Posts: 3061
  • 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
 
author
Sheriff
Posts: 14112
  • 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.
 
Ranch Hand
Posts: 1646
  • 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
Boost this thread!