The use of Java reflection for the interpretation of class classes

Source: Internet
Author: User

Package com.imooc.test;

public class ClassDemo1 {
public static void Main (string[] args) {
Foo fool = new Foo ();//foo1.
Foo This class is also an instance object, the Class class instance object, how to express it
Any class is an instance object of class, and this instance object is represented in three ways
The first representation---> actually tells us that any class has an implied static member variable class
Class cl = Foo.class;
The second expression, known to the class of objects through the GetClass method
Class C2 = Fool.getclass ();
*/* The website explains that C1,C2 represents the class type of the Foo class (class type)
* Everything is the object of everything
* Class is also an object, an instance object of class
* This object we call the class type of the class
*/
Whether C1 or C2 represent the class type of the Foo class, a class can only be an instance object of class
System.out.println (cl = = C2);
The third way of expression
Class C3 = null;
try {
C3 = Class.forName ("Com.imooc.test.Foo");
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}
System.out.println (C2 = = C3);
We can create an instance of this class from the class type of the class, and create instances of Foo through C1.C2.C3.
try {
Foo foo = (foo) cl.newinstance ();//premise requires the construction method without parameters
Foo.print ();
} catch (Instantiationexception e) {
E.printstacktrace ();
} catch (Illegalaccessexception e) {
E.printstacktrace ();
}


}
}
Class foo{
void print () {
System.out.println ("foo print output");
}
}

The use of Java reflection for the interpretation of class classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.