From: "Adam P. Jenkins" Date: 2005-07-21T00:50:56+09:00 Subject: Re: Ruby/Rails as a starter language? Hal Fulton wrote: > Adam P. Jenkins wrote: > >> >> 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 > > > If Foo is a class, what is Foo.class? You have to remember that Java is a statically typed language, and in Java classes are types. In Java you need a way to refer to the Foo type, and another way to refer to the Foo class object. In Java, Foo is a symbol which refers to the type, and Foo.class is an expression which refers to the Foo class object. Since Ruby is entirely dynamically typed, it has no use for separate syntax for type and class, so it can just have a variable of the same name as the class which refers to the class object. Both Java and Ruby have the concept of a class, which has a name, and an object representation. It's just that in Ruby syntax, the class name evaluates to the class object, but in Java the syntax to get at the class object is ClassName.class. > >> Foo foo = new Foo(); // create an instance >> clazz = foo.getClass(); // get instance's class object > > > Can you say this? > > bar = new clazz(); > > And does getClass return you Foo or Foo.class? As explained above, in Java, Foo is a type symbol and doesn't evaluate to any value. Foo.class evaluates to the Foo class object, which is the same object returned by the getClass() method on an instance of Foo. To create an instance from a class object in Java: bar = clazz.newInstance(); Really, you'll need to get into metaobject programming before you can show any real disadvantage of Java over Ruby in this regard other than extra verbosity.