What does classname.class return?

JavaOop

Java Problem Overview


Could anyone please explain what does SomeClassname.class return in JAVA ?? I cant understand what it does ..

Java Solutions


Solution 1 - Java

It returns the same what Object.getClass() does for a given instance, but you can use it when you know statically what class you want (i.e. at compile time).

From the Javadoc:

> Returns the runtime class of this Object.

In short, it gives you an object that represents the class of the (original) object. It's used, amongst other things, by reflection when you want to programatically discover methods and fields in order to invoke/access them.

For example:

        Method m[] = String.class.getDeclaredMethods();
        for (int i = 0; i < m.length; i++)
        {
          System.out.println(m[i].toString());
        }

The Javadoc also refers you to the Java Language Specification - Class Literals (which might be a little heavy reading).

Solution 2 - Java

It returns the Class object that represents the specified class name. This is used if you need to get the Class object.

This roughly corresponds to .getClass() which returns the Class object that corresponds to the object instance. You use someclassname.class when you want to work with the Class object and don't have an object instance.

Solution 3 - Java

.class is a class literal... Just like 5 is an int literal, like 5.0 is a double literal (surprisingly, there is a class named 'Class' in java.lang package).

Therefore you can print the class literal (just like you can print any object... you get what the toString() method returns in the Class class). You can have a Class variable.

PS: there are many functions that you can use

Solution 4 - Java

Object.getclass = className.class

They both return the runtime class of the object

eg

Class lii = SomeClass.class;
Class leo = new SomeClass().getClass();
//lii==leo

Just like

String var1 = "heloo";
int var2 = 9;
float var3 = 8.0;

var1 stores "heloo" which is a String literal, var2 stores 9 which is an integer literal, var3 stores 8.0 which is a float literal, so also lii and leo stores class literal generated from SomeClass.java file at runtime

Solution 5 - Java

Same as .getClass() method, but can be used only for named and void classes. The .class syntax allows to obtain a Class if the type of object is available, but there is no instance. Also, the easiest way to get a class for a primitive type.

Solution 6 - Java

It just like Class object = SomeClass.class; The above statement will return the object of that particular class and will put it in Class Variable. Hens it it wi return Class object.

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
QuestionRajeshwarView Question on Stackoverflow
Solution 1 - JavaGreg KopffView Answer on Stackoverflow
Solution 2 - JavaFrancis Upton IVView Answer on Stackoverflow
Solution 3 - Javauser2067316View Answer on Stackoverflow
Solution 4 - JavaHamzatView Answer on Stackoverflow
Solution 5 - JavaTrent SteeleView Answer on Stackoverflow
Solution 6 - JavaChetan PatilView Answer on Stackoverflow