Java Reflection First lesson understanding class class

Source: Internet
Author: User

Class

1. In an object-oriented world, everything is object

(1) First to understand two questions:

(1) Java voice, static member, normal data type is not an object?

"The normal data type int a = 5 is not object-oriented, but it has a wrapper class to compensate for"

"Java static things are not object-oriented, static things are not an object he belongs to the class."

So in the object-oriented world, everything is the object

(2) whose object is the class?

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

What is the instance object of class? ---the class to which each of our objects belongs is an instance object of class (that is, the instance object of Class Class)

(3) How this object of class is represented


<pre name= "code" class= "Java" >package Com.edu.reflect;public class ClassDemo1 {public static void main (string[] args) {//TODO auto-generated method Stub//foo How the instance object is represented: Foo foo1 = new Foo ();//foo This class is also an instance object, which is an instance object of class classes// Any class is an instance object of class, and how is an instance object of class classes represented? 3 means that the construction method of the//class class is private, only the Java virtual machine can be created, so we cannot directly new The class class instance object//first representation. Actually tell us that any one class has an implied static member variable Classclass c1 = foo.class;//Second expression is known to the object of the class through the GetClass method class C2 = Foo1.getclass (); * * Official website: C1, C2 represents the class type of the Foo class (class type) * Everything is Object * Class is also an object, and is an instance object of the class class * This object we call class type *///regardless of whether C1 or C2 represents the class type of the Foo class, a class can only be an actual class/ Example Object System.out.println (C1==C2);//Result output true//The third way of expression try {Class C3 = Class.forName ("Com.edu.reflect.Foo"); System.out.println (C2==C3);//true} catch (ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} We can create an object instance of the class with the class type of the class by using the try {foo foo = (foo) c1.newinstance ();//need to have a parameterless construction method Foo.print ();} catch ( Instantiationexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (IllegalaccessexcePtion e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} Class Foo{void print () {System.out.println ("Foo class Method");}}



Java Reflection First lesson understanding 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.