Differencebetween A.Getclass() and A.Class in Java

What is the difference between a.getClass() and A.class in 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.

what is the difference between getType() and getClass() in java?

According to the documentation for getClass and getType:

getClass returns "The Class object that represents the runtime class of this object."

getType returns "a Class object identifying the declared type of the field represented by this object"

The main difference being that someObject.getClass() will give you a class object of the runtime type of someObject, and someField.getType() will give you a class object of the declared type of the field that someField refers to.

(Calling someField.getClass() will return Field.class because it's referring to the Field object itself, not the field that it is referring to).

Also, while getClass is available for every object, getType is only available on Field objects which are part of the reflection API.

getclass() vs .class

As per Class javadoc

First approach get Class object from Object.

second approach get Class object for a named type (or for void) using a class literal.

What is the difference between the class Class in the Java API and the class keyword?

In programming, reflection is the ability of a program to modify its own structure and behavior at runtime through analysis of runtime details, such as the actual implementing class of an object instance. The Class class is a part of the Java API for the purposes of reflection. Whereas the class keyword is a structure of the Java language marking the definition of a new class, the Class class is used to type variables and parameters as classes themselves. It's a way for the program to use class definitions themselves as objects to program around (you can programmatically enumerate over the class' public methods, for example).

Additionally, any instance o of type Object (that is, any object in Java at all) inherits the getClass instance method. This yields the actual runtime class of the object, irrespective of the compile-time code-stated class of the variable the object is stored in. For example, for some defined class X:

Object o = new X();
Class<?> type = o.getClass();

type will now be a reference to the X class itself, which matches the Class<?> generic description because the X class is in fact an instance of type Class<X>.

Every defined class also has one static variable called class referring back to the class itself. Calling getClass on an instance of class X will return the same Class<X> instance as that class' class static variable:

("some string").getClass() == String.class

It is worth noting that the primitive types all have a static class variable as well even though they are not Objects. It is also worth noting that a primitive type's class is not the same as the class of its wrapper class:

int.class != Integer.class

Distinction between ClassObject.getClass,ClassName.class and Class.forName(ClassName)

Class.forName loads and initializes the class. obj.getClass() returns the class object loaded into permgen. If the class is loaded by the same classloader == has to return true. When you are see false for == comparison it means that they are loaded by different classloaders.

What's the difference between (Class?) and getClass()?

Your loadMethod appears to already return an object of type Class. So when you call getClass() on that, it's giving you java.lang.Class.class back, not the thing you loaded. Casting the thing you loaded does not change the underlying object, which is the Class instance for your filename.

That is, if you wanted to return the Class Object for the Type Integer, you would:

return Integer.class;

Your first line is doing

return Integer.class.getClass();

so you get the Class object that represents the Type Class itself, not the Type Integer.

What's the difference between calling MyClass.class and MyClass.getClass()

One is an instance method, so it returns the class of the particular object, the other is a Class constant (i.e. known at compile-time).

 Class n = Number.class;
Number o = 1;
o.getClass() // returns Integer.class
o = BigDecimal.ZERO;
o.getClass(); // returns BigDecimal.class

Both cases return instances of the Class object, which describes a particular Java class. For the same class, they return the same instance (there is only one Class object for every class).

A third way to get to the Class objects would be

Class n = Class.forName("java.lang.Number");

Keep in mind that interfaces also have Class objects (such as Number above).

Also, is MyClass.class a public property of the superclass Class class?

It is a language keyword.

Difference between method.getClass().newInstance() and SomeClass.class.newInstance()?

method.getClass() returns Method.class, the class object for Method. This is just like how e.g. "ABC".getClass() returns String.class.

What you are probably looking for is method.getDeclaringClass().



Related Topics



Leave a reply



Submit