How does getClass in Java work

java getclass vs instanceof
java getclass getname
getclass method in java javatpoint
getclass().getname() method in java
get class from string java
java getclass inheritance
java get class name
getname java

Here is what JavaDoc says:

public final Class <?> getClass()

Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class. The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called. For example, no cast is required in this code fragment:

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

Returns: The Class object that represents the runtime class of this object.

Now , I understand it is a native method , so it is 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 query lies in :

  1. Return type of this method
  2. toString method is not called . A similar post on this topic is : Java. getClass() returns a class, how come I can get a string too?
  3. The commented code which otherwise give compile time error.

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

Think of a Class object as the "blueprint" describing a certain class from which objects are being made. It stands to reason that blueprints also need a blueprint of their own (or else how would engineers know how to make blueprints).

These statements 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's 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's class=" + clazzClazzString );

Output:

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

A class has a name, just like anything described by a blueprint has a name which is not to be confused with the blueprint itself. If a class object appears in a certain context, its toString() method is called implicitly, and this returns the class' name. If you'd like to print all the nitty-gritty details of a class (akin to printing the blueprint itself) you'd have to write a lot of code - just look at the javadoc for java.lang.Class: there's an awful lot of information to be retrieved (as befits a blueprint).

How does getClass in Java work, The data for each object contains a reference to an object of class java.lang.​Class, and this is returned by the method getClass. There is also  The focus of this article is the getClass() method, which is used to access metadata about the class of the object you are working with. The getClass() Method. The somewhat confusing or misunderstood Object method getClass() returns an instance of the Class class, which contains information about the class that getClass() was called from. Whew, if you're not confused already by that last statement good for you, because I am and I wrote it!

Java.lang.Object.getClass() Method, The java.lang.Object.getClass() method returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the  Return type of getClass() is a generic Class type. Like many other types available in java - String, Integer etc, Class is also a type representing the type information associated. toString() method is associated and invoked on an instance of the Dog class, not on the Dog type itself. //Class<Dog> dd = new Dog(); Compile time error

I have an answer for your Question 3,

This gives compile time error because

Reason 1: For a Class instance, You can only assign class object that represents the Dog class, but you can't assign the Dog class object directly.

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

Reason 2: The class Class is a final class but not a super class for Dog class. You go back to the concept of dynamic method dispatch in java,where you can only assign subclass objects to a superclass variable.

Java's Object Methods: getClass(), Below are the methods of the base Java Object present in all Java objects due is used to access metadata about the class of the object you are working with. getClass() returns an instance (object) of the Class class. Since each Java class has a single instance of the Class class, if two objects belong to the same class, getClass() for those two objects will return the same isntance and therefore you can use == for comparing them, since == when applied to reference types determines if the two references refer to the same instance.

Retrieving Class Objects (The Java™ Tutorials > The Reflection API , If an instance of an object is available, then the simplest way to get its Class is to invoke Object.getClass() . Of course, this only works for reference types which  Also, you are free to use getClass() in your constructor after the supertype constructor has been called - as you already pointed out, the object is basically ready after that and the Class reference can be inferred from the instance.

Object (Java Platform SE 7 ), getClass. public final Class<?> getClass(). Returns the runtime class of this Object . The returned Class object is the  How getClassLoader().getResourceAsStream() works in java. and find that it searchs the resource in all jar file and zip file in class path.

Writer getClass() method in Java with Examples, The getClass() method of Writer Class in Java is used to get the parent Class of this Writer Below methods illustrates the working of getClass() method:. Object.getClass() If an instance of an object is available, then the simplest way to get its Class is to invoke Object.getClass(). Of course, this only works for reference types which all inherit from Object. Some examples follow.

Comments
  • In java, a Class is an actual valid Object type. Maybe look into the java docs on Class. The commented code gives an error because Class<Dog> should be set to a Class object, while new Dog() returns a Dog object.
  • It's not a native method. As the docs say, an object of type Class merely represents a class. Every Object has a reference to the Class representing its class, and getClass() is just an ordinary getter for it.
  • @KevinKrumwiede , have a look here :stackoverflow.com/questions/26626259/… Now i m confused if it is native or not ?
  • This is the source code from java.lang.Object's class definition: public final native Class<?> getClass();. As the representation of an object is a matter of the implementation, code accessing this implementation-defined data structure would better be "native". There has to be a limit between what doesn't have to be native and what must be native. (See also: Barber's Paradox)
  • @laune Ah... thanks for clarifying that.