Karl von Laudermann wrote:
>mathew wrote:
>>In Java, the word "static" really means "global". A static method is a
>>global function, and a static field is a global variable. Just remember
>>to stick the class name in front when using them.
> 
> Wrong. "static" does not mean "global" in Java, anymore than it does in
> C. Java has no global variables in the sense that C does.

Sure it does. Consider:

Foo.java:

public class Foo {
   static String name = "John";
}

Bar.java:

public class Bar {
   public static void main(String[] argv) {
     System.out.println(Foo.name);
     Foo.name = "Oops";
     System.out.println(Foo.name);
   }
}

Foo.name is a global variable, exactly like those evil global variables 
in BASIC--it's visible to every piece of code, every class loaded by the 
Java run-time system. Sure, it happens to have a '.' in its name, but if 
it looks like a duck, quacks like a duck...

If it helps, rename the Foo class as Global.

> The accessibility of a static member depends on whether it is declared as
> public, protected, package, or private, just like with non-static
> members.  I.e., you simply can't access a *private* static member from
> anywhere outside the class it's declared in. 

Wrong. Change Foo.java above to:

public class Foo {
   static private String name = "John";
}

Run the Bar program again. Guess what it prints?

>>Personally, I find the use of the word 'static' in Java to be confusing;
>>after all, static fields aren't static (as per the usual dictionary
>>definition) at all--their contents can be changed.
> 
> You're confusing "static" with "const" (in C; "final" in Java).

That's exactly my point. It's a confusing use of words.

>>It's also the exact
>>opposite of C, where 'static' means 'local'.
> 
> 
> Wrong. In C, "static" does not mean "local" It means a piece of memory
> allocated on the heap when the program loads, as opposed to being
> allocated on the stack or dynamically.

K&R, page 83, paragraph 1:

"The static declaration, applied to an external variable or function, 
limits the scope of that object to the rest of the source file being 
compiled."

Paragraph 4:

"Normally, function names are global, visible to any part of the entire 
program. If a function is declared static, however, its name is 
invisible outside of the file in which it is declared."

The implementation may or may not involve statically allocated memory, 
but the effect of the keyword, as described, is to limit scope of 
variables and functions. (I'm not even sure offhand how I would tell 
programmatically whether something was in dynamically or statically 
allocated memory.)

Of course, it's possible that ANSI C has changed the language in this 
area; I don't have a copy of the ANSI C specification to check.

> In C, a local variable (declared in a function without the "static"
> keyword) is allocated/deallocated on the stack on the fly as the
> function is called and exits.

You're confusing common implementation with the language itself. Sure, 
that's the reason why static restricts the scope of things which would 
otherwise be global; but that doesn't change the fact that in C, static 
makes file-local that which would otherwise be global. And that is why I 
find it a confusing choice of keyword for Java, where it apparently does 
the opposite.


mathew
-- 
<URL:http://www.pobox.com/~meta/>
          WE HAVE TACOS