15

The way I understand it, Java object model is 3 levels, each level describes the level beneath it, therefore there is one Meta class shared by all Classes (which are themselves objects?).

My question is - how are constructors implemented in Java? (or any other class methods) my logic says that constructors should appear in the Meta classes, but since there is only one Meta class, it doesn't make any sense that it keeps all possible constructors, or is my understanding of this is all wrong..

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
Shmoopy
  • 5,334
  • 4
  • 36
  • 72

2 Answers2

18

In Java there's a single metaclass: the instances of the class Class are used to represent the types of classes and interfaces. The constructors are defined at the class level, not at the metaclass level.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Thanks for replying. Does this mean that constructors are kind of static method added to each class? – Shmoopy Feb 18 '12 at 22:09
  • No, they're neither instance methods nor static methods - they're constructors, a different kind of method whose only purpose is creating new instances of the class – Óscar López Feb 18 '12 at 22:20
10

Your question targets nothing special about constructors: From the point of describing classes on a metalevel there is the same concept for constructors, "normal methods" and fields.

So think of it this way:

  • Each class in Java is described by a certain set of informations:

    • Name of the class
    • the superclass
    • the implemented interfaces
    • a list of constructors and their signatures
    • a list of (static and non-static) methods and their signatures
    • a list of (static and non-static) fields and their types
  • For your convenience this information is available to you during runtime - this is the "reflection API".

  • Since the same type of information is available for each class loaded by the JVM, this is bundled in a own class named java.lang.Class.

  • So one instance of the class Class describes the class java.lang.String, another instance of Class describes my.own.class.Foo.

  • java.lang.Class itself is of course also a class - therefore there also exists an instance of Class describing the class Class. And I think that's where things get recursive somehow.

Summary: There is only one metaclass: java.lang.Class. Multiple instances (meta-instance?) of the metaclass describe individual classes - including the metaclass itself. Constructor descriptions are part of the instances of the metaclass.

A.H.
  • 63,967
  • 15
  • 92
  • 126
  • I understand, thanks. Does this mean that everytime I use 'new String', it creates two instances: one of java.lang.String and one of java.lang.Class? – Shmoopy Feb 18 '12 at 22:29
  • No, because for each instance of `String` the `Class` instance for `String` is just reused. It cannot change or differ between several instances of `String`. So why should _anything_ be copied? – A.H. Feb 18 '12 at 22:43
  • @Shmoopy Only first time you reference String. Only one Class object is created for each class (so one for all of your Strings) – wmz Feb 18 '12 at 22:43