What is a Java class file?

Source: Internet
Author: User
Tags arrays constant constructor field table inheritance reflection static class

Class classes are implementations that define a particular class in the Java language. The definition of a class contains the member variables, the member methods, the interfaces that the class implements, and the parent class of the class. The object of the class class is used to represent 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. Basic Java Types (Boolean, Byte, char, short, int, long, float, and double) and void types can also be represented as Class objects.

  What's the use of class classes?

Instances of class classes represent Java application Run-time classes (class ans enum) or interfaces (interface and annotation) (each Java class runtime behaves as a class object in the JVM, through class name. class, Type. GetClass (), Class.forName ("Class name") to get the class object. The array is also mapped to a class for the class object, and all arrays that have the same element type and number of dimensions share the class object. The basic type boolean,byte,char,short,int,long,float,double and the keyword Void are also represented as class objects.

Second, the class characteristics

The class class does not have a public constructor, which is invoked automatically by the JVM (when the new object or load-classloader).

The following method works by printing out the object's class name:

void Printclassname (Object obj) {

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

"Is" + obj.getclass (). GetName ());


You can also get class name according to class literal:

System.out.println ("The name of Class Foo is:" +foo.class.getname ());//You can change Foo to void.

Three, the main method of class

Class classes are still quite a few ways to do it. It is primarily used to obtain information about Run-time classes (which can be used for reflection).

Several important methods are:

1, public static class forname (String className): Natice method, dynamically loading classes. Very important.

such as dynamically loading drivers in sql: Class.forName (sqldriver);

2,public T newinstance (): Creates a new object for reflection based on the class of the object. Very important.

You can build objects in reflection and invoke object methods:

Class doubleclass= Class.forName ("java.lang.Double");

Object objdouble = Doubleclass.newinstance ();

This method is applied in JavaBean, because Java defaults to a parameterless constructor.

3, Public ClassLoader getClassLoader (): Gets class loader bootstrap, Extension, system or user custom ClassLoader (typically system Classlo Ader). Important.

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

5,public Native Class Getsuperclass (): Gets the parent class of the class, inherits the parent class, returns the parent class, or returns Java.lang.Object. The parent class that returns object is an empty-null. So so

6,public Java.net.URL getresource (string name): A resource is obtained from a string.

7, other classes

public boolean isenum (): Determines whether the enumeration type is.

Public native Boolean IsArray (): Determines whether an array type.

Public native Boolean isprimitive (): Determines whether it is a basic type.

public boolean isannotation (): Determines whether the annotation type is.

Public Package getpackage (): Package is obtained in reflection, such as Package for Java.lang.Object.

public native int getmodifiers (): Get modifiers in reflection, such as public static void.

public field GetField (String name): Gets a domain member in reflection.

Public field[] GetFields (): Gets a domain array member.

Public method[] GetMethods (): Get method.

Public method Getdeclaredmethod (String name, Class ... parametertypes): Add a declared to represent this class, inheritance, and the parent class are not included.

Public constructor[] GetConstructors (): Gets all constructors.

So we know that reflection can dynamically get all the information about the class and create a new object (Newinstance () method) when it runs.

The class file contains the following information:

[+]view Code

  1. By example

[+]view Code

2. Magic number

Role: Determine if the file is a class file acceptable to the virtual machine. Java's magic number is unified 0xCAFEBABE (from a coffee).

Area: File No. 0 to 3rd byte.

3. Version number

Function: Represents the version of the class file, composed of MinorVersion and MajorVersion.

Area: File 4th to 7th byte.

Such as

51 delegates, JDK for 1.7.0

Note that the Java version number is from 45, a large release, major version +1, and a high version of the JDK can be backward compatible with the previous version of the class file, but incompatible with the later version of the class file.

4. Constant pool

The size of the constant pool is not fixed, depending on the number of constants in your class, so at the gate of the constant pool, a U2 type of constant pool capacity counter representing the number of constants in the constant pool is placed. The counter starts at 1 and the No. 0 bit has special meaning, indicating that the index value data pointing to the constant pool does not reference any of the constant pool items. Data items in a pool are accessed through the index like arrays.

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

To hold literal literal and symbol references symbolic References.

Literal literals may be literal strings, or final constant values.

Symbolic references include the following:

Class or interface fully qualified name full qualified name

Field name and descriptor descriptor

Method name and descriptor

Let's use the Decompile tool to see:

[+]view Code

The project types in the constant pool are as follows:

Constant_utf8_info tag Flag bit is 1, UTF-8 encoded string

Constant_integer_info tag Sign bit is 3, shaping literal volume

Constant_float_info tag Flag bit is 4, floating-point literal

Constant_long_info tag Sign bit is 5, long shaping literal amount

Constant_double_info tag Flag bit is 6, double literal

Constant_class_info tag Flag bit is 7, class or interface symbol reference

Constant_string_info tag Flag bit 8, literal string type

Constant_fieldref_info tag Flag bit is 9, the field's symbolic reference

Constant_methodref_info tag Flag bit is 10, symbol reference for method in class

Constant_interfacemethodref_info tag Flag bit 11, symbol reference for method in interface

Constant_nameandtype_info tag Flag bit is 12, field and method name and type symbol reference

  5. Class or interface Access flags

Represents access information for a class or interface, such as class or interface, whether it is public, static, final, and so on. , let's take a look at TestClass's access indicator. The access flag value for class is 0x0021:

According to the previous indications of the various access indicators, we can know: 0x0021=0x0001|0x0020 also namely Acc_public and Acc_super as true, acc_public everyone good understanding, ACC_ Super is a flag that will be jdk1.2 after the class is compiled.

  6. Class index, parent class index, and interface index collection

class file to determine the inheritance of the classes by these 3 data.

Both the class index and the parent class index are constant indexes that point to the constant pool:

  7. Field table Collection

Function: Describes the class variables declared in the interface or class and the instance variables, excluding local variables in the method.

The 2 bytes after the interface index collection are field counters:

Describes the meaning of the Fu Pe-literate character:

V represents a special type of void.

For array types, each dimension will be described with a predecessor "[" character, such as a two-dimensional array of defined "java.lang.string[][]" types, which will be recorded as: "[[Ljava/lang/string;", an Integer array "int[]" Will be recorded as "[I"

The fields in the parent class do not appear in the field table of the child class.

  8. Method table Collection

After the Field table collection is complete, the Method table collection.

Role: Describes the methods in this class.

As with field tables, you use a U2-type method counter to record the number of methods in the class.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.