Difference:
(Recommended tutorial: java introductory tutorial)
class is a keyword in Java, such as public class Xxx or class Xxx, used when declaring Java classes.
Class is a class, which is equivalent to an abstraction and collection of classes.
Class introduction:
Class is a class, which is in the java.lang package.
Its constructor is a private attribute, so we cannot directly create a new Class object. "Private constructor. Only the Java virtual machine creates class objects. This constructor is not used and prevents the generation of the default constructor."
How to get the Class object?
1. Obtain the Class object through the getClass() method
The getClass() method is part of the Object class. If we have created an object of a certain type, we can obtain the Class object of that type through the getClass() method.
package Task; import org.junit.Test; public class Try0 { @Test public void toTry() throws ClassNotFoundException { // //forName方法:参数为其类的路径 // Class a = Class.forName("Task.Try1"); // System.out.println(a); //通过对象得到类 Try1 try1 = new Try1(); Class b = try1.getClass(); System.out.println(b); } } class Try1{ }
Running results:
(Video tutorial recommendation: java video tutorial)
2. Through forName () method to obtain the Class object
Class.forName method is a static method of the Class class. So you can get the Class object directly through Class.forName ("path to the class").
package Task; import org.junit.Test; public class Try0 { @Test public void toTry() throws ClassNotFoundException { //forName方法:参数为其类的路径 Class a = Class.forName("Task.Try1"); System.out.println(a); } } class Try1{ }
Run result:
3. Class.class obtains the Class object (class literal constant)
package Task; import org.junit.Test; public class Try0 { @Test public void toTry() throws ClassNotFoundException { // //forName方法:参数为其类的路径 // Class a = Class.forName("Task.Try1"); // System.out.println(a); // //通过对象得到类 // Try1 try1 = new Try1(); // Class b = try1.getClass(); // System.out.println(b); //类字面常量 Class c = Try1.class; System.out.println(c); } } class Try1{ }
Run result :
##
The above is the detailed content of What is the difference between class and Class in Java?. For more information, please follow other related articles on the PHP Chinese website!