Sunday, 21 February 2016

Java Class Linking and Initialization

My previous blog explains how a class is loaded. Once Java class is compiled, it’s binary is loaded into the JVM. Next two phase are Linking and Initialization.

Linking


Linking involves three steps.
Verification
Preparation
Resolution.

Verification:

One of the advantage of java architecture is that once a java source file is compiled and converted to bytecode it can be executed in any JVM. This also introduce security risk as the source is untrustworthy. So verification is like a security guard which check the byte code before it is allowed to be executed by JVM. Verifier is a part of VM and programmer does not have any control over it.

  
    

Preparation:

Memory allocation and initialization of class variable to default value is done at this steps
Two important point is class variable are assigned initial value later and the value for Boolean is not false but integer zero(0).

  

Resolution:

  
Resolution involves resolving symbolic reference of classes, interfaces, fields and methods with direct reference.
"symbolic references" are strings that can be used to retrieve the actual object
  

    Class and Interface resolution





  Field Resolution



  
  
Exception :
      NoSuchFiledError : Field that cannot be resolved
      IllegalAccessError : Field is not accessible
  
  

Method Resolution:




Exception
    NoSuchMethodError: Method Resolution Fails.
    AbstractMethodError: Method resolution succeed but method is abstract and class is not abstract
    IllegalAccessError:  Field is not accessible


Initialization

Once linking is completed the last phase Initialization is executed. In summary this phase assign initial value to class variable and static initializers.

JVM compiles the java class and collects the class variable and static initializer into a no name methods call “”. Programmer does not have any control on this special methods and only VM execute this method to assign initial value to class variable and static initializer


This special methods is called Class Initialization method for class and Interface initialization method for interface.

Note: If a java class does not have class variable or static initializer then the compiled class will not have no name special method “”.

Class Initialization
Initialize the class’s direct superclass if not initialized and recursively keep on going up unless all the superclass in the hierarchy is initialize or reached at the top of hierarchy that is object class
Initialize the class Initialization method.

Interface Initialization
1. Initialize the interface Initialization method.

Additional resource can be found here



No comments:

Post a Comment

Streaming with Kafka API

The Kafka Streams API is a Java library for building real-time applications and microservices that efficiently process and analyze large-sca...