Two Laptop Bag
The moose likes Servlets and the fly likes Static & Class Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Static & Class Variables " Watch "Static & Class Variables " New topic

Static & Class Variables

Chandar S Vel

Joined: Aug 11, 2002
Posts: 10
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.
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
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.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Feb 14, 2001
Posts: 285
I thought 'static' variable implied a class variable.
Can you explain the difference?
kapil Gupta
Ranch Hand

Joined: Dec 17, 2001
Posts: 89
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.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
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.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695


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

Joined: Feb 14, 2001
Posts: 285
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.
I agree. Here's the link:
subject: Static & Class Variables
It's not a secret anymore!