[ACCEPTED]-How Java linker works?-specifications

Accepted answer
Score: 22

There is no such thing as a Java "linker". There 7 is, however, the concept of a classloader 6 which - given an array of java byte codes 5 from "somewhere" - can create an internal 4 representation of a Class which can then 3 be used with new etc.

In this scenario interfaces 2 are just special classes. Methods and fields 1 are available when the class has been loaded.

Score: 14

First of all: methods are always part of 33 a class. Interfaces are basically just special 32 classes, and packages are just a part of 31 the fully qualified name of a class with 30 some impact on visibility and the physical 29 organization of class files.

So the question 28 comes down to: how does a JVM link class 27 files? The JVM spec you linked to says:

The 26 Java programming language allows an implementation 25 flexibility as to when linking activities 24 (and, because of recursion, loading) take 23 place, provided that the semantics of 22 the language are respected, that a class or 21 interface is completely verified and prepared 20 before it is initialized, and that errors 19 detected during linkage are thrown at 18 a point in the program where some action 17 is taken by the program that might require 16 linkage to the class or interface involved 15 in the error.

For example, an implementation 14 may choose to resolve each symbolic reference 13 in a class or interface individually, only 12 when it is used (lazy or late resolution), or 11 to resolve them all at once, for example, while 10 the class is being verified (static resolution). This 9 means that the resolution process may 8 continue, in some implementations, after 7 a class or interface has been initialized.

Thus, the 6 question can only be answered for a specific 5 JVM implementation.

Furthermore, it should 4 never make a difference in the behaviour 3 of Java programs, except possibly for the 2 exact point where linking errors result 1 in runtime Error instances being thrown.

Score: 10

Java doesn't do linking the way C does. The 8 principle unit is the class definition. A 7 lot of the matching of a class reference 6 to its definition happens at runtime. So 5 you could compile a class against one version 4 of a library, but provide another version 3 at runtime. If the relevant signatures match, everything 2 will be ok. There's some in-lining of constants 1 at compile time, but that's about it.

Score: 3

As noted previously Java compiler doesn't 22 have a linker. However, JVM has a linking 21 phase, which performed after class loading. JVM 20 spec defines it at best:

Linking a class 19 or interface involves verifying and preparing 18 that class or interface, its direct superclass, its 17 direct superinterfaces, and its element 16 type (if it is an array type), if necessary. Resolution 15 of symbolic references in the class or interface 14 is an optional part of linking.

This specification 13 allows an implementation flexibility as 12 to when linking activities (and, because 11 of recursion, loading) take place, provided 10 that all of the following properties are 9 maintained:

  • A class or interface is completely 8 loaded before it is linked.

  • A class or interface 7 is completely verified and prepared before 6 it is initialized.

  • Errors detected during 5 linkage are thrown at a point in the program where 4 some action is taken by the program that 3 might, directly or indirectly, require 2 linkage to the class or interface involved 1 in the error.


Score: 0

Linking is one of the three activities performed 13 by ClassLoaders. It includes verification, preparation, and 12 (optionally) resolution.

Verification : It ensures the 11 correctness of .class file i.e. it check 10 whether this file is properly formatted 9 and generated by valid compiler or not. If 8 verification fails, we get run-time exception 7 java.lang.VerifyError.

Preparation : JVM allocates memory 6 for class variables and initializing the 5 memory to default values.

Resolution : It is the process 4 of replacing symbolic references from the 3 type with direct references. It is done 2 by searching into method area to locate 1 the referenced entity.

More Related questions