What is the difference between a.getClass() and A.class in Java?

JavaClassReflection

Java Problem Overview


In Java what pros/cons exist surrounding the choice to use a.getClass() or A.class? Either can be used wherever a Class<?> is expected, but I imagine that there would be performance or other subtle benefits to using both in different circumstances (just like there are with Class.forName() and ClassLoader.loadClass().

Java Solutions


Solution 1 - Java

I wouldn't compare them in terms of pros/cons since they have different purposes and there's seldom a "choice" to make between the two.

  • a.getClass() returns the runtime type of a. I.e., if you have A a = new B(); then a.getClass() will return the B class.

  • A.class evaluates to the A class statically, and is used for other purposes often related to reflection.

In terms of performance, there may be a measurable difference, but I won't say anything about it because in the end it is JVM and/or compiler dependent.


This post has been rewritten as an article here.

Solution 2 - Java

They are actually different with regards to where you can use them. A.class works at compile time while a.getClass() requires an instance of type A and works at runtime.

There may be a performance difference as well. While A.class can be resolved by the compiler because it knows the actual type of A, a.getClass() is a virtual method call happening at runtime.

For reference, a compiler targeting bytecode typically emits the following instructions for Integer.getClass():

aload_1
invokevirtual	#3; //Method java/lang/Object.getClass:()Ljava/lang/Class;

and the following for Integer.class:

//const #3 = class	#16;	//  java/lang/Integer

ldc_w	#3; //class java/lang/Integer

The former would typically involve a virtual method dispatch and therefore presumably take longer time to execute. That is in the end JVM-dependent however.

Solution 3 - Java

have a look at the examples below

a.getClass()!= A.class, i.e. a is not an instance of A but of an anonymous sub class of A

a.getClass() requires an instance of type A

Solution 4 - Java

Use a.getClass when you have an instance of class/type and you want to get exact type of it. while a.class is used when you have type available and you want to create instance of it.
Also getClass() returns runtime type of instance while .class is evaluated at compile time.
Considering performance of getClass() and.class ,.class has better performance than getClass() .
Example :

public class PerfomanceClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		long time=System.nanoTime();
		Class class1="String".getClass();
		class1="String".getClass();
		class1="String".getClass();
		class1="String".getClass();

		System.out.println("time (getClass()) :"+(System.nanoTime()-time)+" ns");	  


		long time2=System.nanoTime();
		Class class2=String.class;
		class2=String.class;
		class2=String.class;
		class2=String.class;

		System.out.println("time (.class):"+(System.nanoTime()-time2)+" ns");
	}

}

Output :

time (getClass()) : 79410 ns
time (.class)     : 8032 ns

Solution 5 - Java

There is one difference i would like to add. Let us say you have a class a constructor as shown below with a super class which takes a Class object. You want that whenever a subclass object is created the subClass' class object should be passed to the super class. Below code will not compile as you cannot call an instance method in a constructor. In that case if you replace myObject.getClass() with MyClass.class. It will run perfectly.

Class MyClass
{
    private MyClass myObject = new MyClass();
    public MyClass()
    {
        super(myObject.getClass()); //error line compile time error
    }
}
        

Solution 6 - Java

Interestingly the differences in performance mentioned in the example above, seem to be related to other reasons. Using 3 different Classes, in average the performance will be nearly the same :

import java.util.LinkedHashMap;
public class PerfomanceClass {

public static void main(String[] args) {

    long time = System.nanoTime();
    Class class1 = "String".getClass();
    Class class11 = "Integer".getClass();
    Class class111 = "LinkedHashMap".getClass();

    System.out.println("time (getClass()) :" + (System.nanoTime() - time) + " ns");

    long time2 = System.nanoTime();
    Class class2 = String.class;
    Class class22 = Integer.class;
    Class class222 = LinkedHashMap.class;

    System.out.println("time (.class):" + (System.nanoTime() - time2) + " ns");
} }

The Output will be something like :

time (getClass()) :23506 ns 
time (.class):23838 ns

And switching the order of the calls will even result in getClass() being faster.

import java.util.LinkedHashMap;

public class PerfomanceClass {

public static void main(String[] args) {
    long time2 = System.nanoTime();
    Class class2 = LinkedHashMap.class;

    System.out.println("time (.class):" + (System.nanoTime() - time2) + " ns");

    long time = System.nanoTime();
    Class class1 = "LinkedHashMap".getClass();

    System.out.println("time (getClass()) :" + (System.nanoTime() - time) + " ns");
}}

Output:

time (.class):33108 ns
time (getClass()) :6622 ns

Solution 7 - Java

p.getClass(), where the p is an instance of an object, returns the runtime class of this object p. p cannot be a type which will cause a compile-time error, it should be an instance of an object.

// B extends A
A a = new B();
System.out.println(a.getClass());
//output: class B

p.class is an expression. The .class is called the class syntax. p is a type. It can be the name of a class, interface or array and even a primitive type. a.getClass() == B.class.

If a type is available and there is an instance then it is possible to use getClass method to get the name of the type. Otherwise, use the .class syntax

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIAmYourFajaView Question on Stackoverflow
Solution 1 - JavaaioobeView Answer on Stackoverflow
Solution 2 - JavaTomasz NurkiewiczView Answer on Stackoverflow
Solution 3 - JavaMassimiliano PelusoView Answer on Stackoverflow
Solution 4 - JavaRohanView Answer on Stackoverflow
Solution 5 - JavaprashantView Answer on Stackoverflow
Solution 6 - Javauser4287932View Answer on Stackoverflow
Solution 7 - JavatriplestoneView Answer on Stackoverflow