#: Gavin Kistner changed the world a bit at a time by saying on  8/21/2005 4:36 PM :#
> On Aug 21, 2005, at 3:11 AM, Alexandru Popescu wrote:
>> Coming from the Java world, for me a class method is a (static)  
>> method available withouth a self (this) reference, but it is  
>> available/callable from any instance. (is this available in Ruby?)
> 
> Sorry, I don't know Java (any more). Are you describing a global  
> function that is only available to instance methods of a certain  
> class, which is invoked without an explicit receiver?
> 

Nope. I was talking about:
[code]
public class ExampleClass {
	public static void staticMethod() {
		// illegal access to any instance field
		System.out.println("staticMethod");
	}
}

ExampleClass.staticMethod(); // prints the message; no instance of ExampleClass is available
ExampleClass ec= new ExampleClass();
ec.staticMethod(); // prints the message; not recommended but works

> 
> 
>> It seems that in Ruby the class method is a method belonging to the  
>> class ClassExample object.
> 
> Yup, that's the case. Classes are objects too. So you can do:
> 
> class Foo
>      def self.upcount   #same as def Foo.upcount
>          @instances ||= 0
>          @instances += 1
>      end
> 
>      def self.getcount
>          @instances
>      end
> 
> 
>      def initialize
>          self.class.upcount
>      end
> 
>      def count_neighbors
>          self.class.getcount
>      end
> end
> 
> f1 = Foo.new
> f2 = Foo.new
> f3 = Foo.new
> p f1.count_neighbors #=> 3
> 
> 
> As you can see above, the Class has its own scope for its own  
> instance variable. The class itself is a completely separate instance  
> of the "Class" class, which just happens to be tied to instances of  
> it in a few special ways.
> 

Writting some more examples I think I got the idea. It is subtle difference that I don't know how to 
describe in words :-s. (the class method in Ruby belongs only to the new defined class, but not to 
its instances, while in Java the class method belongs to the new defined class and all its instances).

:alex |.::the_mindstorm::.|