Java Basics-Reflection (very important) __java

Source: Internet
Author: User
Tags reflection

Java Basics-Reflection (very important)

* * Original blogger * *: http://blog.csdn.net/sinat_38259539/article/details/71799078Reflection is the soul of frame design(Use Prerequisites: The Class,class class that must first get the byte code represented is used to represent the. class file (bytecode))
I. Overview of ReflectionThe Java reflection mechanism is in the running state, for any class, all the properties and methods of the class can be known, and any one of its methods and properties can be invoked for any object; This dynamically acquired information, and the function of the method that dynamically invokes the object, is called the reflection mechanism of the Java language.
To dissect a class, you must first obtain the byte-code file object for that class. And anatomy uses the methods in class classes. So get to the object of the class type for each byte-code file first.


The above summary is what is reflection reflection is the Java class to map the various components of a Java object such as: A class has: member variables, methods, construction methods, packets and so on information, the use of reflection technology can be a class anatomy, the components of each mapping into a single object. (actually: In a class, these member methods, construction methods, and a class in the join class are described) as shown in the normal loading process of a class: the principle of reflection is in relation to the class object. Familiar with the loading time: The class object is to read the class file into memory, and create a class object for it.


This class object is very special. Let's get to the C lass class first.
second, view class class in Java API detailed (1.7 API)How to read Java API see--string string processing in Java basics


Instances of class classes represent classes and interfaces in a running Java application. That is, there are more than n instances in the JVM and each class has the class object. (including basic data types) Class has no public constructor method. Class objects are automatically constructed by the Java virtual machine and by invoking the DefineClass method in the ClassLoader when loading the class. That means we don't have to deal with the creation ourselves, the JVM has helped us create it.

There are no public constructs, there are 64 or so many methods. Which one is the following?


third, the use of reflection (here using the Student class to do demo)Write a student class first.
1, get the class object three ways 1.1 object--> getclass ();
1.2 Any data type (including basic data types) has a "Static" Class attribute
1.3 Static method Via Class class: Forname (String className) (commonly used)

1.1 of these are due to the GetClass method in the object class, because all classes inherit the object class. Thus invoking the object class to get the

[java]  View plain  copy <span style= "font-size:18px;" >package fanshe;  /**   *  Three ways to get class objects    * 1 Object  --> getclass ();   * 2  any data type (including basic data types) has a "Static" Class attribute    * 3  Static methods through class classes: forname (string  classname) (common)    *   */   Public class  Fanshe {       public static void main (string[]  args)  {           //the first way to get the class object               student stu1 = new student ()//This new   produces a student object, a class object.            class stuclass = stu1.getclass ()// Gets the class object            system.out.println (Stuclass.getname ());    &Nbsp;                  // The second way to get Class objects            Class stuClass2 =  student.class;           system.out.println (stuClass == &NBSP;STUCLASS2)//judge the first way to get the class object and the second way to get whether the same                        //Third Way to get class objects             try {                class stuclass3 = class.forname ("Fanshe.") Student ");/Note that this string must be a true path, a classpath with a package name, a package name. class name              &NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (STUCLASS3&NBSP;==&NBSP;STUCLASS2)//To determine whether three ways to get the same class object            } catch  (classnotfoundexception e)  {                e.printstacktrace ();           }                   }  } </span>   [java]  View Plain  copy

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.