Java-> Class & Object, java-class

Source: Internet
Author: User

Java-> Class & Object, java-> class

In Java, we often see a type: Class. It can also be seen in Code such as Person. class and cache. getClass.

As we all know, Class is used to describe the type of a Class, and Object is the final parent Object of all objects. The following two conclusions will be drawn:

1. From the Object point of view, it must first have an Object, and then have its derived Object Class.

2. Class indicates the Class and Object. It must have the concept of Class first, followed by various types (abstract and non-Abstract), including objects.

This will lead to a problem: Is there a Class first or an Object first )?

 

Okay, here we will give the answer first. There is an Object first and then a Class.

The reason is that the Object is the final parent Object of all objects, and the Class itself is also an Object. Therefore, the Class Object is created first.

So how to explain the second point? This is because a concept is obfuscated.

In Java, all objects are derived from objects, and Class classes (note that uppercase is also a Class) also inherit from objects, in eclipse, we can clearly view the inheritance relationships of classes.

In Java, there is also a class (note that it is in lower case ). He is one by one (anti-theft connection: This article first from the http://www.cnblogs.com/jilodream/) object, that is, a class. Objects are one of these objects. At the same time, there is an object in these objects. It is used to identify the content of other objects. This Class is called Class (note that it is capitalized ). Therefore, a class is called Class. In this case, class is equivalent to Class, which is obviously unreasonable. The two are not in one dimension.

Therefore, it is easy to understand that after JVM is started, it will load all the required classes, including the class.

 

Discussion ---- in-depth study of Class

After learning about the relationships between class, Object, and Class, let's take a closer look at the Class class. (This is the main purpose of this blog)

I. background knowledge

Class objects are loaded by JVM before they are used (in fact, class initialization is completed by loading, connecting, and initializing ). Class loading means that the JVM reads the Class file into the memory and creates a class Object for it. At the same time, when a class is loaded and used again, it will not be repeatedly added (anti-theft connection: the first self-http://www.cnblogs.com/jilodream/) load. In this way, the newly created Class has a one-to-one correspondence with the loaded class. With this Class object, you can access the corresponding class. So we can regard the Class as a Class Identifier object, which is equivalent to a Class label (nameplate ). After getting a Class, we can find the corresponding class ).

Ii. Methods for obtaining Class objects

In Java, we can use three methods to get the Class object. Two of them are for Class objects that are already at home to obtain their corresponding Class objects. The remaining one uses reflection to find the corresponding class file based on the provided Class name, and then finds the class object.

 

1 Class. forName (String className) // className indicates the complete name, including the package name of this Class. If it cannot be found, this method will throw a 2 Person. class // Person indicates a class, and the class field is its default attribute 3 person. getClass () // getClass is an instance method of the Obj Class. All classes have this method, including the Class.

 

3. information that can be obtained from the Class

The system can find the Class corresponding to this object through the class object, and the Class object contains the basic details of the class. The information can be divided into the following four aspects: 1. Get the constructor contained in the class. 2. Obtain the methods contained in the class. 3. Obtain the member variables contained in the class. 4. Obtain the Annotation contained in the class. Ps many friends may not be familiar with Annotation. Here, Annotation is translated as an Annotation, which is also a class and can be used to save the class description information. If you are interested, refer to the following article:

Http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html

Iv. Usage of Class classes in work

Here I have summarized the usage situations that I have encountered and divided them into two aspects. If there are any omissions, you can add them.

1. Use and verification of Object Types

In some cases, we need to verify the type of the input object to determine whether the input object is of the type we need.

If (para. getClass = Person. class) // if the instance keyword is used here, it may be subject to interference from the inheritance relationship of the Person class, resulting in incorrect judgment.

 

2. Reflection

<1> use a string to define the name of the class to be loaded, and then load it when necessary.

This method has three purposes:

(1) Sometimes I don't know the type to be loaded here. I need to know which class to be loaded at runtime. For example, during the running process, according to the user's manual settings, dynamically select the next class to be loaded.

(2) The name of the class to be loaded is known during compilation, but there is no name to be loaded. class file, which must be uploaded by the user at runtime or downloaded from the specified address in the background. The implementation of plug-in development is to use this principle. Here are two examples:

(α) when using filtering, You need to define a complex filtering mechanism by yourself. In this case, you may not be able to simply set the content to be filtered through the interface. You can manually upload the jar package of your own filtering algorithm, and then dynamically load it in the background using this algorithm.

(β) You can also display a set of default display effects on the skin in a working environment, and also upload the jar package for display effects on your own. After obtaining the jar package uploaded by the user, the background reflects the special effect algorithm to form dynamic interaction.

(3) Shorten Compilation Time and speed up software startup (including client and server)

At startup, the class containing the main method is loaded, and it will load (anti-theft connection: This article first from the http://www.cnblogs.com/jilodream/) the class you need. These classes are loaded again. Form a recursive relationship. However, for a large application, the whole startup (loading) process takes a long time, which is often intolerable to users and is forced to close even before loading.

In this case, we can load some of the most basic classes in the mian method class. Such as logon and verification. After the logon verification is complete, you need to perform business operations to load the classes required by the user based on the user's selection. This improves the overall operating efficiency and user experience of the software.

<2> development and use of tools

When developing tools or scripts, in addition to using the open APIs of the system, we sometimes need to use some of the variables and methods that were private in the original code. In this case, only inheritance is not enough. You also need to reflect the object, get the variable or call the method. For example, the UT framework that is commonly used sometimes provides many APIs that can directly call private methods of the classes to be tested for test efficiency.

 

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.