Metaclasses

From Suhrid.net Wiki
Revision as of 07:48, 17 December 2011 by Suhridk (talk | contribs) (Created page with "* Each object belongs to a class. * But, Everything is an object. * So, classes are '''objects''' apart from being classes. <syntaxhighlight lang="java5"> String s = "Hello"; C...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
  • Each object belongs to a class.
  • But, Everything is an object.
  • So, classes are objects apart from being classes.
String s = "Hello";
Class stringClassObj = s.getClass();
  • So the class object (stringClassObj) is an instance of which class ?
  • It is an instance of java.lang.Class.
  • java.lang.Class extends java.lang.Object since classes are objects.
  • So java.lang.Class is a metaclass. Instances of java.lang.Class are classes.
  • A class defines the behaviour of its instances. Similarly, a metaclass defines the behavior of the classes that are its instances.
  • E.g. java.lang.Class has methods that defined the behavior of its instances.
    • e.g. getName(), getDeclaredFields(), getMethods(),
  • e.g. getMethods return objects which are of type Method class. Instances of java.lang.reflect.Method are methods.
  • The method class defines behavior of all methods, for e.g. getReturnType(), getModifiers() etc.