What is a Java Class file?

Source: Internet
Author: User
Tags field table

What is a Java Class file?

The implementation of a specific Class is defined in Java. A class definition includes member variables, member methods, interfaces implemented by the class, and the parent class of the class. The Class object is used to represent the classes and interfaces in the currently running Java application. For example, each array belongs to a Class object, and all arrays with the same element type and dimension share a Class object. The basic Java types (boolean, byte, char, short, int, long, float, and double) and void can also be expressed as Class objects.

  I. What is the use of the class?

An instance of the class represents the class (class ans enum) or interface (interface and annotation) when the java application is running (each java class is represented as a class object in the JVM, you can use the class name. class, type. getClass (), Class. forName ("class Name") and other methods to obtain the class object ). Arrays are also mapped to a class Object. All arrays with the same element type and dimension share the Class object. The basic types include boolean, byte, char, short, int, long, float, double, and void.

Ii. Features of the class

The class does not have a public constructor. It is automatically called by JVM (when a new object or a-classLoader is loaded ).

The following method prints the class name of the object:

Void printClassName (Object obj ){

System. out. println ("The class of" + obj +

"Is" + obj. getClass (). getName ());

}

You can also obtain the class name based on class literal:

System. out. println ("The name of class Foo is:" + Foo. class. getName (); // you can change Foo to void.

Iii. Main Methods of class

There are still many class methods. It is mainly used to obtain information about the runtime class (which can be used for reflection ).

Important methods:

1, public static Class forName (String className): natice method, dynamic loading Class. Very important.

For example, dynamically loading the driver in SQL: class. forName (sqlDriver );

2, public T newInstance (): creates an object based on the class of the object for reflection. Very important.

You can build an object in reflection and call the object method:

Class doubleClass = class. forName ("java. lang. Double ");

Object objDouble = doubleClass. newInstance ();

For example, this method is applied to javaBean, Because java requires a non-argument constructor by default.

3. public ClassLoader getClassLoader (): obtains the class loaders Bootstrap, Extension, System or user custom ClassLoader (generally system classloader) of the class ). Important.

4, public String getName (): gets the name of a class or interface. Remember that enum is a class and annotation is an interface. Important

5. public native Class getSuperclass (): gets the parent Class of the Class. If the parent Class is inherited, the parent Class is returned; otherwise, java. lang. Object is returned. The returned Object's parent class is null. Average

6. public java.net. URL getResource (String name): Obtains resources based on strings.

7. Other classes

Public boolean isEnum (): determines whether it is of the enumeration type.

Public native boolean isArray (): determines whether it is of the array type.

Public native boolean isPrimitive (): determines whether it is of the basic type.

Public boolean isAnnotation (): determines whether the annotation type is used.

Public Package getPackage (): Get the package in reflection. For example, the package of java. lang. Object is java. lang.

Public native int getModifiers (): Get modifier in reflection, such as public static void.

Public Field getField (String name): obtains the Domain Member from reflection.

Public Field [] getFields (): obtains the Field array member.

Public Method [] getMethods (): Obtain the Method.

Public Method getDeclaredMethod (String name, Class... parameterTypes): Add a Declared to represent the Class, inherit, and the parent Class is not included.

Public Constructor [] getConstructors (): obtains all constructors.

In this way, we can know that reflection can dynamically obtain all information about the class at runtime, and create an object (newInstance () method ).

The Class file contains the following information:

[+] View code

  1. From the instance perspective

[+] View code

2. Magic number

Purpose: Determine whether the file is a class file acceptable to the virtual machine. Java has a uniform magic number of 0 xCAFEBABE (from a coffee ).

Area: file 0th ~ 3 bytes.

3. Version Number

Role: indicates the version of the class file, which consists of minorversion and majorversion.

Area: file 4th ~ 7 bytes.

For example

51 representative, jdk is 1.7.0

Note that the java version number starts from 45. The major version is released, and the major version number is + 1. jdk of a higher version is backward compatible with class files of earlier versions, But not later versions.

4. Constant pool

The size of the constant pool is not fixed, depending on the number of constants in your class, so in the entry of the constant pool, put a u2 type constant pool capacity counter that represents the number of constants in the constant pool. The counter starts from 1 and has a special meaning of 0th BITs, which indicates the index value data pointing to the constant pool.Do not referenceAny constant pool project. The data items in the pool are accessed through indexes like arrays.

We can clearly see that our constant pool has 63-1 = 62 constants. What are these constants?

To store Literal and Symbolic References to Symbolic References.

The literal may be a text string or a constant value of final.

Symbol references include the following:

Full Qualified Name

Field name and Descriptor

Method Name and Descriptor

Let's use the decompilation tool to check the following:

[+] View code

The project types in the constant pool are as follows:

CONSTANT_Utf8_info the tag flag is 1, A UTF-8-encoded string

The CONSTANT_Integer_info tag is 3, which is a literal integer.

The CONSTANT_Float_info tag flag is 4, float literal

The CONSTANT_Long_info tag is 5 characters long integer literal

CONSTANT_Double_info the tag flag is 6, double-precision literal

The CONSTANT_Class_info tag flag is 7, and the class or interface symbol reference

The CONSTANT_String_info tag flag is 8, which indicates the string type.

The CONSTANT_Fieldref_info tag flag is 9, and the field symbol reference

CONSTANT_Methodref_info the tag flag is 10, and the symbol reference of the method in the class

The CONSTANT_InterfaceMethodref_info tag is 11, and the symbol reference of the method in the interface

CONSTANT_NameAndType_info the tag flag is 12. The name and type of the field and method are referenced.

  5. Class or interface access flag

Indicates the access information of the Class or interface, such as whether the Class indicates the Class or interface, whether it is public, static, final, etc ., Next let's take a look at the access ID of TestClass. The Class access flag value is 0x0021:

According to the access signs mentioned above, we can know that: 0x0021 = 0x0001 | 0x0020, that is, ACC_PUBLIC and ACC_SUPER are true. In this example, ACC_PUBLIC is easy to understand, the ACC_SUPER class is compiled after jdk1.2.

  6. Class index, parent index, and interface index set

In the Class file, these three data items are used to determine the inheritance relationship of the Class.

Both the class index and the parent index point to the constant index in the constant pool:

  7. Field table set

Purpose: Describe the class variables declared in the interface or class and instance variables, excluding the local variables in the method.

The second byte after the interface index set is the field counter:

Description:

V indicates a special type of void.

For array types, each dimension is described using a prefix "[" character, such as a defined "java. lang. string [] [] "two-dimensional array of the type, which will be recorded as:" [[Ljava/lang/String ;", an integer array "int []" will be recorded as "[I"

Fields in the parent class do not appear in the field table of the subclass.

  8. method table set

After the field table set ends, it is the method table set.

Purpose: Describe the methods in this class.

Like a field table, a u2 method counter is used to record the number of methods in this class.

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.