• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Static & Class Variables

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I need to know one basic thing in a servlet. What is the difference between a Class Variable and a static variable in a Servlet (Servlet that does not implement SingleThreadModel Interface).
Thanks a Bunch!!!
Chandar S. Vel.
 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static and class variable are one and the same thing. They are shared by all instances of the servlet, and are never thread-safe, even if the SingleThreadModel interface is implemented (multiple threads may access the same static variable from different servlet instances).
Perhaps you wanted to know the difference between class and instance variables in a servlet? Instance variables are also not thread-safe as they can be shared by more than one thread servicing the same servlet instance. They are thread-safe, however, if the SingleThreadModel interface is implemented since only one thread may service an instance at a time.
Hope this helps.
Mark.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Umm... class variables are NOT shared by all instances of the class, so 'static' and 'class' are not the same thing.

If you snip your first sentence and re-word the 2nd sentence to begin "Static variables", I think you'd be 100% correct.
[ January 27, 2003: Message edited by: Mike Curwen ]
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh
I thought 'static' variable implied a class variable.
Can you explain the difference?
Thanks
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was just musing if static functions can only call other static functions then how the main method of a class can call constructor which is not a static method.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike - I have always seen "class variable" and "static" used interchangably with "instance" variable as clearly different. Admittedly "class variable" is a sloppy usage when "static" is clearer, but that is the way I have seen it used.
Bill
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
doh.

you know, i waffled back and forth about posting. I knew there was something not right about the post. Was I completely insane? Or was I right? I got caught up in terminology. Sigh. Is it a bad thing that I'm supposed to write my cert. exam on Friday?

Right you are Bill and mark. 'static implies class'. My thinking error came about because in my head I was saying "variables at the level of the class" (as opposed to "method level"). Which in *THAT* thinking space, could be either static or not.

I knew something was fishy. Sorry for the confusion.
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No sweat Mike, do it all the time myself.
A bit like the words "lose" and "loose", which must be the most commonly misused words in the English language. But you get so used to their misuse that the difference becomes unclear after a while. You certainly had me having a hard look at the words "static" and "class" again
Good luck for Friday.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
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