From: Jim Weirich Date: 2003-08-22T21:26:10+09:00 Subject: Re: Class variables - a surprising result On Thu, 2003-08-21 at 15:14, Mark J. Reed wrote: > On Fri, Aug 22, 2003 at 03:47:33AM +0900, Yukihiro Matsumoto wrote: > > As far as I know, C++ and Java do not have class variables. They have > > static member variables, > > .. . . which are not inherited, which is why it is surprising to many > that Ruby's @@variables are inherited. Hmmm ... it looks like they are inherited here ... // Begin Example ------------------------------------------------------ class A { static int avar = 0; } class B extends A { } public class Demo { public static void main(String args[]) { System.out.println ("A.avar = " + A.avar); // => 0 System.out.println ("B.avar = " + B.avar); // => 0 B.avar = 10; System.out.println ("A.avar = " + A.avar); // => 10 System.out.println ("B.avar = " + B.avar); // => 10 } } // End Example ------------------------------------------------------- (Sorry for posting Java code in a Ruby mailing list). The truth is that Java doesn't have "class variables" (although they are sometimes called that). Java has static variables that are scoped by the class. If you redeclare a variable in B, then it will hide the variable in A. Since you don't declare variables in Ruby, you don't get the "hiding" effect. -- -- Jim Weirich jweirich@one.net http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)