I see Java has only one metaclass (the Class class), but other languages, say Smalltalk, have one metaclass for each Class.
Why is that? What’s the need for metaclasses? What difference does it make to have them one way or another?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The fundamental need for at least one metaclass is that if you want objects that represent classes (or want classes to be objects), then those objects must have a type.
Wikipedia says:
So the question is, do you want every class object to have the same type (and hence the same members), or do you want class objects to differ in ways that require them to have different types, so that there are type-checked operations which can be performed on the object that represents class A but not on the object that represents class B? Java and early Smalltalks answered that question differently from later Smalltalks.
So for example
java.lang.Class.newInstance()takes no constructor arguments, whereas you can imagine that it might be nice to be able to callclz.newInstance(1)whereclzis the class object for a class that has a constructor with takes anint. In Java you can still look through the constructors of the class yourself to find a match for the arguments you want to pass, but the type of the class object doesn’t tell you whether you will find one.Also note that Smalltalk stops at one level. The type of
CisC class, but the type ofC classisMetaclass. There’s no infinite recursion of typesC class classetc, because although different class objects in Smalltalk accept different messages, there’s no demand for different metaclass objects to accept different messages.