Search code examples
javajvmjvm-hotspotpermgen

What is Klass & KlassKlass


What is Klass & KlassKlass in JVM Hotspot Implementation?

As far as I understood from the article Presenting the Perm Generation, Klass is the internal representation of a Java class (let's say A) & it will hold the basic information about the structure of the class including the bytecode. It will be stored as an object itself. Every object of the class A will have a pointer to the internal representation Klass present in PermGen

KlassKlass is the internal representation of the Klass class itself. Why does KlassKlass is needed? What extra information is it storing?

Also, a KlassKlass's Klass pointer points to itself, I didn't understand it either.


Solution

  • Permanent Generation a.k.a permgen is the referred to the place where all the class related information is stored. It is occasionally referred as method-area.

    Lets take an example of the following code:

    public class Parent
    {
        ...
    }
    

    Here:

    • new Parent() is an object of Parent class.
    • (new Parent()).getClass() refers to Klass of Parent. The reference type for this object would be java.lang.Class<Parent>. This would store the information about Parent's annotations, constructors, fields, methods, its inheritance (superclass, interfaces) etc
    • KlassKlass would be (new Parent()).getClass().getClass(). The reference type for this object would be java.lang.Class<java.lang.Class>. This defines the information about java.lang.Class's annotations, constructors, fields, methods, its inheritance (superclass, interfaces), etc.

    Theoretically, this chain could go on but KlassKlassKlass would be same as KlassKlass.

    Inshort, KlassKlass means that you would have one java.lang.Class object which defines the behavior of java.lang.Class itself.

    Hope this helps