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 variable with global visibility. 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.

Same goes for methods. If I can call it from any piece of code in my 
program, without having to import it or declare it, then it's a global 
function. The fact that the name has a '.' in it doesn't change its 
visibility.

 > 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.

Sure, you can override static with private. But that doesn't really 
change the fact that static fields have global visibility unless 
explicitly made private.

 >> 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 full 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 eliding common implementation and the language itself. Sure, 
that's the reason why static originally restricted 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 
static things are not file-local, but are (unless modified by private) 
program-global.


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