Home > Java > javaTutorial > What are the three instantiations of Class in java

What are the three instantiations of Class in java

WBOY
Release: 2023-05-04 20:01:04
forward
995 people have browsed it

1. Call the getClass() method in the Object class:

import java.util.Date;
    public class ReflectTest3 {
public static void main(String[] args) {
Date date = new Date();
Class<?> cls =  date.getClass();
System.out.println(cls);
}
    }
    "class java.util.Date"
Copy after login

2. Use "class.class" to obtain:

import java.util.Date;
    public class ReflectTest4 {
public static void main(String[] args) {
Class<?> cls =  Date.class;
System.out.println(cls);
}
    }
"class java.util.Date"
Copy after login

3. Call a method provided by the Class class - instantiate the Class object

    public class ReflectTest5 {
public static void main(String[] args) throws ClassNotFoundException {
Class<?> cls =  Class.forName("java.util.Date");
System.out.println(cls);
}
    }
    "class java.util.Date"
Copy after login

The above is the detailed content of What are the three instantiations of Class in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template