Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does classname.class return?

Tags:

java

oop

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

like image 972
Rajeshwar Avatar asked May 07 '12 06:05

Rajeshwar


People also ask

What is meaning of classname class in Java?

Java provides a class with name Class in java. lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

How do I get a classname class?

You can use: Class c = Class. forName("com.

What is the use of class class in Java?

Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.

What is classname class in PHP?

Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class . This is particularly useful with namespaced classes.


2 Answers

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).

like image 101
Greg Kopff Avatar answered Oct 12 '22 16:10

Greg Kopff


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.

like image 20
Francis Upton IV Avatar answered Oct 12 '22 14:10

Francis Upton IV