How getClass works in Java

Here is what JavaDoc says:

public final Class <?> getClass() 

Returns the execution class of this Object . The returned Class object is an object that is locked by the static synchronized methods of the represented class.
Actual result type Class<? extends |X|> Class<? extends |X|> , where |X| - This erases the static type of the expression on which getClass is called. For example, a throw is not required in this code fragment:

 Number n = 0; Class<? extends Number> c = n.getClass(); 

Return:
A class object representing the execution class of this object.

Now I understand that this is a proprietary method, which is why it is implemented in platform-dependent code. But what about the return type of this method.

 public final Class<?> getClass() 

Also consider the code:

 class Dog { @Override public String toString() { return "cat"; } } public class Main { public static void main(String[] args) { Dog d= new Dog(); //Class<Dog> dd = new Dog(); Compile time error System.out.println(d.getClass()); } } 

Output:

class Dog

So, my request is enclosed in:

+8
source share
3 answers

The data for each object contains a reference to an object of the java.lang.Class class, and this is returned by the getClass method. There is also one java.lang.Class object that describes java.lang.Class.

Think of the Class object as a "plan" that describes the specific class from which the objects are created. Of course, drawings also need their own drawings (or how engineers know how to draw drawings).

These statements will try to illustrate this.

 Integer integer = 1; Class<?> clazzInteger = integer.getClass(); System.out.println( "class of integer=" + clazzInteger ); Class<?> clazzClazzInteger = clazzInteger.getClass(); System.out.println( "class of class Integer class=" + clazzClazzInteger ); String string = "x"; Class<?> clazzString = string.getClass(); System.out.println( "class of string=" + clazzString ); Class<?> clazzClazzString = clazzString.getClass(); System.out.println( "class of class String class=" + clazzClazzString ); 

Output:

 class of integer=class java.lang.Integer class of class Integer class=class java.lang.Class class of string=class java.lang.String class of class String class=class java.lang.Class 

A class has a name, just like anything described in a plan has a name that should not be confused with the plan itself. If a class object appears in a specific context, its toString () method is called implicit, and this returns the name of the class. If you want to print all the small details of the class (akin to printing the drawing itself), you have to write a lot of code - just look at javadoc for java.lang.Class: there is an awful lot of information to get (as it should be in the plan).

+7
source

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; //Type information associated with type `A` A instanceOfA = new A(); //actual instance of type `A` } } 

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

+1
source

I have an answer to your question 3,

This gives a compile-time error because

Reason 1. For an instance of a class, you can only assign an object of the class that represents the Dog class, but you cannot assign an object of the Dog class directly.

For example: Class dd = Dog.class or Class dd = Class.forName ("Dog"); correct syntax.

Reason 2: Class is the last class, but not a superclass for Dog. You return to the concept of dynamic method allocation in Java, where you can only assign subclass objects to a superclass variable.

0
source

All Articles