Java Programming Thought study notes-14th chapter type Information

Source: Internet
Author: User

Run-time type information allows you to discover and use type information while the program is running.

There are two main ways: one is "traditional" RTTI, which assumes that we already know all the types at compile time, and the other is the "reflection" mechanism, which allows us to discover and use the information of the class at run time.


Class object

A class is part of a program, and each class has an object of class. In other words, whenever a new class is written and compiled, it produces a class object (more appropriately, it is stored in a. class file of the same name). To generate objects of this class, the Java Virtual Machine (JVM) running the program will use a subsystem called the ClassLoader.

All classes are dynamically loaded into the JVM when they are used for the first time. This class is loaded when the program creates the first static member reference to a class. This proves that the constructor is also a static method of the class, even though the static keyword is not used before the constructor. Therefore, a new object that creates a class using the new operator is also treated as a reference to a static member of the class.

The Java program is not fully loaded until it starts running, and its parts are loaded when required.

The class loader first checks to see if the class object is already loaded. If it is not already loaded, the default ClassLoader will find the. class file based on the class name. When the bytecode of this class is loaded, they are validated to ensure that it is not corrupted and does not contain bad Java code

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


Methods for obtaining Class objects: Class.forName ("ClassName"), Object.getclass (), Class.class;


Class literal constants

such as: Toy.class

Class literals are not only simple but also more secure because they are checked at compile time (and therefore do not need to be placed in a try statement block). And it has eliminated the call to the forname () method, so it is also more efficient.

Class literal constants can be applied not only to ordinary classes, but also to interfaces, arrays, and basic data types.

When you use ". Class" To create a reference to a class object, the class object is not automatically initialized. The preparation to use the class actually consists of three steps:

1. Load, this is performed by the ClassLoader. This step looks for bytecode (typically found in the path specified by Classpath, but is not required) and creates a class object from those bytecode.

2. Link, in the link phase, validates the bytecode in the class, allocates storage space for the static domain, and, if necessary, resolves all references to other classes created by this class.

3. Initialize, if the class has a superclass, initialize it, execute static initializers, and static initialization blocks.

Initialization is deferred until the static method (the constructor is implicitly static) or the first reference to a very few static domain is performed.


If a static final constant is a compile-time constant, then this value does not require initialization of the class to be read.

If a static domain is not final, it is always required to link (allocate storage space for the domain) and initialize (initialize the storage space) before it is accessed.



interface and type information

An important goal of the interface keyword is to allow programmers to isolate artifacts and thus reduce coupling. If you write an interface, you can do this, but with type information, the coupling will still propagate-the interface is not an airtight guarantee of decoupling.

Dynamic Agent

Use reflection to see anything in the class.


This article is from the "Small City Ops" blog, please be sure to keep this source http://lixcto.blog.51cto.com/4834175/1911877

Java Programming Thought study notes-14th chapter type Information

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.