mathew wrote:
> Mark Volkmann wrote:
> > Is there some difference between what is called a "class method" in
> > Ruby and what is called a "static method" in Java?  They seem the same
> > to me.

Don't think of them as "static methods" in Java. Think of them as class
methods, which they are; it's just that "static" is the keyword that
Java uses to declare class methods and fields, because that's what C++
used.

> 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. 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. Thus, such a member is
certainly not "global" in any sense.

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

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

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. If you add the "static" keyword before
it, then the variable is allocated on the heap when the program
executes, and exists for the life of the program. Thus, the function
can store data in it that persists between function calls, so you could
for example count how many times the function was invoked. The fact
that it can only be accessed from within the function is because of
where the variable is declared, not because of the "static" keyword.
Similarly, a "static" variable declared outside a function is local to
the file it appears in. This is as opposed to "extern", so in this
sense you could make a case for it meaning "local", but what "static"
really means in this context is that it is defined when it is declared.
An extern can have multiple declarations, but must have exactly one
definition.

C++ then added a new context for "static"; when used to declare a field
in a class, it creates a variable which has only one (persistent)
instance for the entire class. As opposed to one instance per class
instantiation, allocated and deallocated as objects of that class are
allocated and deallocated.

> Did it really not occur to
> the developers of Java that it would be clearer to use the word 'global'?

The developers of Java felt that it would be clearer to use the same
keyword that C++ uses.

> So remember: final means static, and static means global...

Or not. "final" means "const", and "static" means allocated once, when
the class is loaded. The closest equivalent Java has to a global
variable is a *public* static member field, and even then you have to
use the containing class' name as a namespace to access it.