5

What is the reason behind using the word "static" in "static variables" or "static methods"?

I'm not asking for the definition of "static", just asking why it is called so.

  • well if you know the definition of static, you would understand why they are called static variables and static methods... – Darkhydro Feb 21 '11 at 03:50
  • because they dont change... they are **static** – Scott M. Feb 21 '11 at 03:52
  • 2
    @Scott, are you seriously suggesting that `static int x = 2;` can never change the value to 3? – paxdiablo Feb 21 '11 at 03:55
  • no, but `static int x` will always be in the same place when you go to look for it. – Scott M. Feb 21 '11 at 04:15
  • Well, actually, so will the non-static during it's lifetime. Your `static int x` may be in a totally _different_ place next time it's instantiated (i.e., when you re-run the program). Static is really only whether or not something is class-specific or object-specific. Even under C, where the concept came from, static has nothing to do with location. _One_ of its meaning is static _value_ (not location) in terms of maintaining values between invocations of a function but all others are neither unchanging value nor location. – paxdiablo Feb 21 '11 at 04:33

4 Answers4

11

It's a hold over from C++ and C before that. In the C context, one of its meanings was for variables that keep their value between invocations. I presume that's where 'static' comes from - it doesn't reset the value (as opposed to a const where the value cannot change)

Kaffiene
  • 705
  • 4
  • 12
  • +1 - That's as close as we are likely to get. The original reasons why `static` was chosen, and what other alternatives were considered are probably only known to the original Oak team ... if they can still remember from 20+ years ago. – Stephen C Feb 21 '11 at 03:58
  • 3
    Actually, static is overloaded in C. The definition you give is when you use it within a function where it controls duration. It's also used outside of functions to limit the visibility of something to the current translation unit. In C++, it has a third use within a class to create a variable shared amongst all instances. See http://stackoverflow.com/questions/943280/difference-between-static-in-c-and-static-in-c/943303#943303 but +1 for the most likely explanation. – paxdiablo Feb 21 '11 at 03:58
6

I suppose the best way to think of it is that "static" means unchanging, and it is the location of the variable that is unchanging when when you switch between the different instances of a class. The main thing is to eradicate any thought from your mind that the value of a static variable is unchanging: it may even be changed by another instance of the same class.

Elroch
  • 132
  • 1
  • 7
1

In Head First Java, there is a nice explanation of static. Static means stable not changing thats obvious from meaning and its quite obvious that it does not change.

Static mean sharing in Java. There is a nice picture where two child sharing one ice cream and showed static variable analogy. For more info please read Head First Java.

Umesh K
  • 13,436
  • 25
  • 87
  • 129
-3

Definition of STATIC 1 : exerting force by reason of weight alone without motion 2 : of or relating to bodies at rest or forces in equilibrium 3 : showing little change 4 a : characterized by a lack of movement, animation, or progression b : producing an effect of repose or quiescence 5 a : standing or fixed in one place : stationary b of water : stored in a tank but not under pressure

just apply the definition to a variable...

Scott M.
  • 7,313
  • 30
  • 39
  • 2
    I can't tell if you're trying to be funny or not. _None_ of those definitions match static in Java – paxdiablo Feb 21 '11 at 03:56
  • @Software, if you mean at one location, that's rubbish. _All_ variables are fixed during their lifetime, even non-static file level and automatic variables in C, as well as object variables in Java. – paxdiablo Feb 21 '11 at 04:05
  • 1
    All static means in Java is that something belongs to the class rather than an object. I can't see _anything_ even remotely close to that in this answer. The definition seems to lean towards `const` rather than `static`. – paxdiablo Feb 21 '11 at 04:08
  • obviously the variables are water stored in a tank but not under pressure. but seriously, i do think this question is a bit silly. – Scott M. Feb 21 '11 at 04:15
  • I asked this question because of a line in wikipedia : _"..In other words, data in a static or global variable is normally always located at the same memory location"_ http://en.wikipedia.org/wiki/Thread-Specific_Storage – securitypolicymanager Feb 21 '11 at 04:18
  • 1
    in light of the additional information: that article is for unmanaged languages, really. Java abstracts most of that for you. – Scott M. Feb 21 '11 at 04:28