What does .class mean in Java?

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

What does .class mean in Java? For example, if I created a class called Print. What does Print.class return?

回答1:

When you write .class after a class name, it references the class literal - java.lang.Class object that represents information about given class.

For example, if your class is Print, then Print.class is an object that represents the class Print on runtime. It is the same object that is returned by the getClass() method of any (direct) instance of Print.

Print myPrint = new Print(); System.out.println(Print.class.getName()); System.out.println(myPrint.getClass().getName()); 


回答2:

.class is used when there isn't an instance of the class available.
.getClass() is used when there is an instance of the class available.

object.getClass() returns the class of the given object. For example:

String string = "hello"; System.out.println(string.getClass().toString()); 

This will output

class java.lang.String 

This is the class of the string object :)



回答3:

If an instance of an object is available, then the simplest way to get its Class is to invoke Object.getClass()

The .class Syntax

If the type is available but there is no instance then it is possible to obtain a Class by appending .class to the name of the type. This is also the easiest way to obtain the Class for a primitive type.

boolean b; Class c = b.getClass();   // compile-time error  Class c = boolean.class;  // correct 

See: docs.oracle.com about class



回答4:

Just to clarify, this '.class' method is not referring to the bytecode file you see after compiling java code nor a confusion between the concepts of Class vs. Object in OOP theory.

This '.class' method is used in Java for code Reflection. Generally you can gather meta data for your class such as the full qualified class name, list of constants, list of public fields, etc, etc.

Check these links (already mentioned above) to get all the details:
https://docs.oracle.com/javase/tutorial/reflect/class/classNew.html
https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html

Normally you don't plan on using Reflection right away when you start building your project. It's something that you know you need after trying to manage already working code. Many times you need it to manage multiple instances of your program. Maybe you want to identify each particular 'clone' to determine if something is already defined, or count the number of functions, or just simply log the details of a particular instance of your class.



回答5:

If there is no instance available then .class syntax is used to get the corresponding Class object for a class otherwise you can use getClass() method to get Class object. Since, there is no instance of primitive data type, we have to use .class syntax for primitive data types.

    package test;      public class Test {        public static void main(String[] args)        {           //there is no instance available for class Test, so use Test.class           System.out.println("Test.class.getName() ::: " + Test.class.getName());            // Now create an instance of class Test use getClass()           Test testObj = new Test();           System.out.println("testObj.getClass().getName() ::: " + testObj.getClass().getName());            //For primitive type           System.out.println("boolean.class.getName() ::: " + boolean.class.getName());           System.out.println("int.class.getName() ::: " + int.class.getName());           System.out.println("char.class.getName() ::: " + char.class.getName());           System.out.println("long.class.getName() ::: " + long.class.getName());        }     } 


回答6:

I think the key here is understanding the difference between a Class and an Object. An Object is an instance of a Class. But in a fully object-oriented language, a Class is also an Object. So calling .class gets the reference to the Class object of that Class, which can then be manipulated.



回答7:

A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a '.' and the token class. One of the changes in JDK 5.0 is that the class java.lang.Class is generic, java.lang.Class Class<T>, therefore:

Class<Print> p = Print.class; 

References here:

https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html

http://docs.oracle.com/javase/tutorial/extra/generics/literals.html

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.8.2



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!