• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Class Variables --

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got a nitpick about using a class variable when I could be using one declared within a method ( if you understand ? ). Is this a preference thing or is there some important point that I am missing ?

Am I right to say that a method variable will only persist while the method is being invoked while a class variable will hang around until the class is no longer needed ?
[ July 19, 2004: Message edited by: Justin Simpson ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key is that class variables are to be used as attributes for a class. If there is no intention to instantiate the class, there is no need for attributes.

Here is a discussion of the same issue. Although the student was on a different assignment at the time, the concepts are the same.

A local (method) variable persists until the method is exited. A class (static) variable persists until the JVM unloads the class (usually when the JVM is shut down). An object member variable persists until it is garbage collected.
[ July 19, 2004: Message edited by: Marilyn de Queiroz ]
 
Justin Simpson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marilyn
 
today's feeble attempt to support the empire
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic