Java Reflection--class Class

Source: Internet
Author: User

In an object-oriented world, everything is object.

1) whose object is the class?

A class is an object, and a class is an instance object of the Java.lang.Class class.

2) How is this object represented?

 PackageCom.reflect; Public classClassDemo1 { Public Static voidMain (string[] args) {//how Foo objects are representedFoo foo1=NewFoo ();//f001, that's it.//Foo This class, is also a total instance object, the Class class instance object, how to express it? //any total class is an instance object of class, and this instance object is represented in three ways//The first representation--"actually tells us that any total class has an implied static member variable class Class C1=foo.class; //The second expression is known to the object of the class, through the GetClass methodClass c2=Foo1.getclass (); //Official website C1,C2 indicates the class type of the Foo class ( class type)//Everything is Object,//class is also an object, an instance object of class//This object we call class type of class//whether C1 or C2 represent the class type of the Foo class, the total class can only be a total instance object of classSystem.out.println (c1==C2); //The Third Way of expressionClass c3=NULL; Try{C3=class.forname ("Com.reflect.Foo"); } Catch(ClassNotFoundException e) {e.printstacktrace (); } System.out.println (C2==C3); //We can create instance objects of the class--> by the class type of the class, by creating an instance of Foo by C1 or C2 or C3.        Try{Foo Foo2= (Foo) c1.newinstance ();//need to have a parameterless construction methodFoo2.print (); } Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); }            }}classfoo{voidprint () {}}

Java Reflection--class Class

Related Article

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.