Reprint Please specify the Source:Jiq ' s technical Blog
1 Class File:
The full name of the class file, called the Javaclass file, makes Java more suitable for networking in terms of platform independence and network mobility. Its mission in terms of platform independence is to provide Java programs with a binary form of service that is independent of the underlying host platform.
Each class has an object of class type, and whenever a class is compiled, it produces a class object (saved in a. class file of the same name);
2 Class file load time:
Load on demand at run time.
Unlike a normal program, a Java program (class file) is not a local executable program. When running a Java program, the JVM is run first, then the Java class is loaded into the JVM as needed at runtime, and this part of the load Java class is called Class Loader.
Three scenarios trigger class loader to dynamically load class objects to the JVM:
(1) The first static method is called;
(2) The first time is instantiated by new;
(3) contains static code blocks;
Once a class object is loaded into memory, it is used to create all objects of the class;
3 RTTI Three forms of expression:
Java is the use of class objects to implement the RTTI mechanism, Java Rtti has three forms of expression:
1). Traditional type conversion.
As the base class is the subclass class base class.
Base base = Newsubclass ();
Subclass Sub= (subclass) base;//determined by Rtti type conversion correctness
2). You can get the information required by the runtime by using a class object that represents the type of the object.
3). The keyword "instanceof" determines the object type.
such as: Boolean Yesornot = (baseinstanceof subclass);
If base is an instance of the subclass class, Yesornot is true.
4 RTTI Limitations:
There is a word in the book: The difference between RTTI and reflection is that for RTTI , the compiler opens and checks the . Class file at compile time , For the reflection mechanism,the. Class file is not available at compile time, so the. class file is opened and checked at run time .
How can this be understood???
Rtti knows all the types to be processed during compilation, but sometimes the runtime needs to parse an object, such as you get a string of bytes in a disk file or network, and be told that the bytes represent a class. Unfortunately, there is no related action in the code that triggers the corresponding class file to be loaded into the JVM, which is called the runtime to open and check the. class file.
5 Reflection Mechanism
It is then necessary to explicitly tell the JVM which class file to load to obtain the corresponding type information.
There are two ways to obtain a reference to a class object that is loaded first if it is not loaded:
(1) Method Class.forName ("ClassName")
(2). class literal constants.
The difference is that the latter defers the initialization of the class (execution of the static area) to the subsequent reference to the static domain of the class;
Class<?>classobject = Class.forName ("ClassName");
Method[]methods = Classobject.getmethods ();
Constructor[]ctors = Classobject.getconstructors ();
Thus, class is the basis of Rtti, and the class class and the Java.lang.reflect class library support the concept of reflection.
6 non-security of reflection
Even if you are programming interface-oriented, only provide "want to see the parts" to the client, but you still cannot avoid your invisible private, or not private all methods are used by the client's fate, you cannot prevent such a coupling. The client can break through any of your defenses with the anti-compilation + reflection mechanism. But if someone does it, it's their responsibility.
7 Spring the dependency injection in
Dependency Injection in spring is achieved by relying on the reflection mechanism.
First spring will read those beans that are configured in the configuration file, and then get the class name of the object to be injected, so that it can be based on:
Classs bean = Class.forName ("ClassName");
Objectoo = Bean.newinstance ();
This creates an object for the class to be injected.
We can use the reflection mechanism to get all the methods that inject the target action, including its Setxxx method, so we can then call
This method of Method#invoke (method name, parameter) is used to inject the created object into it.
8 create an instance using the reflection mechanism:
(1) Create a normal instance:
(2) Create a one-dimensional array:
(3) Create a two-dimensional array:
Java Basics: Rtti and reflection