Java Class Object

Source: Internet
Author: User

Java Class Object

@author Ixenos

Keywords: RTTI, dynamic bind, dynamic load, get class reference, generic class reference, newinstance pit

Rtti and dynamic binding

Rtti is run-time type-aware run-time type identification or run-time type information

For example, when a Shape object is placed in an array of list<shape>, it is transformed upward, but when it is transformed to shape, it loses the type of the Shape object, and for arrays they are just shape objects. When you remove an element from the list<shape> array, the container (which actually holds all the elements as object) automatically transforms the result back to Shape, which is the most basic form of use of Rtti, Because all type conversions in Java are checked for correctness at runtime, this is what Rtti name means: At run time, identify the type of an object.

In this example, the Rtti type conversion is not exhaustive, because at this time we only know that this list<shape> is a Shape, which is guaranteed by the container and the generic system at compilation , and is guaranteed by the type conversion operation at run time .

and the type is converted to the previously written polymorphic (dynamic binding) thing, because this time needs to invoke the method of the object, dynamic binding to determine the domain and method of the call

Summary:Rtti Identifies the type of an object at run time, after which the polymorphic determines the actual type of the object at run time to determine the invocation

Class object and dynamic loading

To understand how Rtti works in Java, you must first know how the type information is represented at run time, and this work is done by the class object

The class object is used to create all the regular objects of the class, each of which has a class object, and each time a new class is compiled, a class object (saved in a. class file) is generated, as shown in:

1. Class Loader SUBSYSTEM:

To generate the object of the class, the JVM uses the class loader subsystem (Classloader) to load the . class file (the contents are bytecode)

class loader chain: This subsystem can usually contain a class loader chain, but only a native ClassLoader, which (referred to as a subsystem) is part of the JVM implementation

native ClassLoader: the native ClassLoader loads the so-called trusted classes that they typically load from local disks (including Java API classes)

extra ClassLoader: In this chain there is usually no need to add additional class loaders , but if the class is loaded by special requirements (in a special way to support Web server applications, or to download classes on the network)

2. Dynamic Loading:

All classes (Xxx.class) are dynamically loaded into the JVM when they are used for the first time. When a program creates the first reference to a static object member of a class, it loads This class (such as calling static methods, static fields) 

1 Class Candy {2     Static{System.out.println ("Loading Candy"); }3 }4 5 Class Gum {6     Static{System.out.println ("Loading Gum"); }7 }8 9 Class cookie{Ten     Static{System.out.println ("Loading Cookie")); } One } A  -Publicclasssweetshop { -      Public Static voidMain (string[] args) { theSystem.out.println ("main"); -         NewCandy (); -System.out.println ("After creating Candy"); -         Try{ +Class.forName ("Gum"); -}Catch(ClassNotFoundException e) { +System.out.println ("Gum not founded"); A         } atSystem.out.println ("After creating Gum"); -         Newcookies (); -System.out.println ("After creating Cookie"); -     } - } -  in---------------------------------- - Output: to Main + Loading Candy - After creating Candy the Loading Gum * After creating Gum $ Loading CookiesPanax NotoginsengAfter creating cookies
Dynamic Loading

The output shows that the class object is loaded only when needed, and static initialization occurs when the class is loaded.

The result of the call to Class.forName () shows that if the class gum has not been loaded, the static clause of gum is executed during the loading process.

When the object is constructed only by the constructor, the class loads, indicating that the constructor is also a static method of the class , although there is no static keyword decoration. Therefore, new objects that create classes using the new operator are also treated as references to static members of the class

Therefore, theJava program (which contains many classes) is not fully loaded until it starts to run, and its various parts (. Class) are loaded when required

3. Class loader for dynamic loading:

The class loader first checks to see if the class object is already loaded.

if it is not loaded , the default ClassLoader is to find the. class file by type (for example, an extra classloader might look up a byte code (the contents of a. class file) in the database)

if loaded , the class object will be validated to ensure that it is not corrupted and contains no bad code (Java security Precautions)

 

4. Once a class object is loaded into memory, it is used to create all objects of the class

Class gets a reference to a class object 0. Preface

The preparation to use the class consists of three steps:

1) load : class loader looks up bytecode (typically found in classpath), creates a class object from bytecode

2) link : Verify bytecode, allocate storage space for static domain (only static decorated domain, no static final), resolve all references to other classes for this class

3) Initialize : If the class has a superclass, initialize it, execute the static initializer (constructor count one) and static initialization block .

-----------------------------

static final int = 47 is a compile-time constant that does not require initialization of the class to read

static final int = Random.nextint is a run-time constant, which is generally not run until the object is created, beyond the initialization stage!

static int = 47 is not a constant, it's not a const, it's not a compile-time constant

Forname static methods for 1.Class classes (automatic initialization)

Dynamically generate Class<string> objects using Class.forName (String name) only if the corresponding type name is known .

2.Object class object comes with GetClass method (already initialized)

when holding a reference to a corresponding type object, use the object's GetClass () (part of the root class object), such as the new Integer (1). GetClass () The Integer.class is returned, and the type object at this time must be running, so it has been initialized

3. Use class literal constants (not automatically initialized)

such as Fancy.class, String.class, Integer.type

1) class literal constants apply to: ordinary Class (including wrapper class Yo), interface, array, basic data type

The base data type uses the Standard field type, which is a reference to a class object that points to the base data type

For example, Int.class is equivalent to Integer.type, but not equivalent to Integer.class

2) When you use ". Class" To create a reference to a class object, the class object is not automatically initialized.

So why not initialize it automatically? By supplemental content, initialization is deferred until it is executed for the static (statically) method (the constructor is equivalent to implicit static) or the first reference to a very few (final) static domain, while the reference class literal constants are run at runtime only to the loading and linking stages

Generic class Reference 1. Function:

1) qualify the type of class object that the class reference refers to (that is, the type parameter represents the type of the class, such as Class<t> = T.class)

2) Let the compiler enforce additional type checking

2. Wildcard characters:

To loosen the limit when using a generalized class reference, use wildcards, which are part of the Java generics

1)class<?>: superior to ordinary Class, even if they are equivalent,class<?> if a non-specific reference is used, the compiler will alert

2)class< extends t>: To create a Class reference that is limited to a type (or subtype of that type), use wildcards to combine with the extends keyword to create a range

1  Public classboundclass{2      Public Static voidMain (string[] args) {3class<?extendsnumber> bound =int.class;4bound =Double.class; The class object can be referenced5bound = number.class;//can refer to the class object6     }7}

3. class's newinstance ()Method:

1) The Newinstance method of the ordinary class object that returns an object of type

2) Newinstance method for ordinary generic class object that returns the exact type of object

3)newinstance () pit : If the class is in the superclass, the wildcard type is modified by Super < Super Fancytoy>, returns an object of Type objects

Because the compiler will only allow you to declare that the superclass reference is "a class, it is a superclass of Fancytoy", this ambiguity is defined so that only object can be returned for security

1 Super fancytoy> up = ftclass.getsuperclass (); 2 // The following compilation does not pass: 3 // class<toy> up2 = Ftclass.getsuperclass (); 4 5 // only object is returned 6 Object obj = up.newinsance ();

The compilation does not pass because Getsuperclass returns a superclass reference, and the superclass reference must be a vague definition by class<? Super T> receive

Getsuperclass
Getsuperclass ()
Returns a superclass representing the Class entity (class, interface, base type, or void) represented by this representation Class . If this Class represents Object a class, an interface, a base type, or void, NULL is returned. If this object represents an array class, the object representing the Object class is returned Class .
Return:
The
superclass of the class represented by this object.

Java Class Object

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.