Devin Mullins wrote:
> Daniel Amelang wrote:
> 
>>> In Java, classes aren't objects.
>>>   
>>
>> Sorry Hal:
>>
>> http://java.sun.com/docs/books/tutorial/reflect/class/index.html
>>  
>>
> Close, but no cigar. Classes aren't objects. *Sure* there is this 
> magical Class object which you can use to get access to magical Field 
> objects and Method objects, but that means that java.lang.String.class 
> is an object, not java.lang.String. Which means, in the most practical 
> sense, that Daniel Brockman's little polymorph example isn't possible, 
> as written. (Yeah, you could pull it off if you require the client to 
> pass in a String of the qualified name, or the corresponding Class 
> object, and if you make extensive use of the Reflection API, but that 
> just proves that classes aren't objects.)
> 
> Am I being pedantic? Maybe. But it's certainly enough of a difference 
> for me to notice, and like Ruby for it.

Yes you are being pedantic.  In Java classes certainly are objects, it's 
just that the syntax for getting at the class object is a little 
different from Ruby:

In Java:

class Foo {}  // define a class
Class clazz = Foo.class;  // assign class to a variable
Foo foo = new Foo();  // create an instance
clazz = foo.getClass();  // get instance's class object

In Ruby:

class Foo; end # define a class
clazz = Foo # assign class to a variable
foo = Foo.new # create an instance
clazz = foo.class # get instance's class object

Not so different, see?  And you can even implement the polyorph example 
in Java as well, though it's a little more verbose.  The extra verbosity 
is mostly due to static typing though rather than any lack of reflection 
support.

Object polymorph(Object snufkin) {
   Method m = snufkin.getClass().getMethod("foo", null);
   return m.invoke(snufkin, null);
}

You'll need to add a throws clause to the above method, but otherwise 
that's all there is to it in Java.  You'll need to start getting into 
metaobject programming before you can show any convincing advantages of 
Ruby over Java in this regard.

Adam