At this point, we need to distinguish between type and instance types. Let's explain this with an example.
public class A { public static void main(String[] args) { Class<A> typeInformation = A.class;
A type
The link "typeInformation" in the above code is of type Class , keeping aside the generics for some time. This information will usually be in a memory section without a heap. The following information is stored against each of type jvm downloads:
- Full Type Name
- The full name of the direct type superclass (unless the type is an interface or class java.lang.Object, none of which have a superclass)
- Regardless of whether the type is a class or an interface
- Type modifiers (some subsets of `public, abstract, final)
- An ordered list of full names of any direct superinterfaces
Instance
instaneOfA is a reference to an actual instance of type A that points to an address in heap memory.
The return type getClass () is a generic Class type. Like many other types available in java - String, Integer, etc., Class is also a type representing related type information.
The toString () method is bound and called on an instance of the Dog class, and not on the Dog type itself.
//Class<Dog> dd = new Dog(); Compile time error
This is due to type mismatch when assigning the result of an expression on the right side of the left side of a side that is not of the same type. The dd class refers to a class type reference. The dog is a completely different type, and a new dog () can be assigned a link like "Dog".
This link will help you understand java runtime design aspects
source share