On Jul 23, 2005, at 1:31 AM, mathew wrote:


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

In Java, when you don't specify private, protected, or public, you  
get the default visible--which is package (a lot like friend in C++).  
Foo.name is only visible to Bar because they're in the same package,  
not because the static keyword makes the variable in question global.  
Put either of your examples in a different package and it (name) is  
no longer visible.

Java doesn't have global variables. Java does provide for sharing  
among classes in the same package if you choose not to specify more  
restricted access. You can implement a class that's a singleton, but  
there's no keyword to make it so.


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

He's not overriding static, he's overriding the default "package"  
visibility. They're two completely different things. In Java, static  
means that there's a single copy shared by all instances of the  
class. Nothing more. It has nothing at all to do with the variable's  
visibility outside the owning class.


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

Agreed. final means that the value can only be set once (and, in  
fact, must be set once or the compiler will complain)--in other  
words, it's constant, once set.


> <snip>
>


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


Trey