the Other neighbourhood

Never can he ever sleep without the fear of being robbed, assaulted or captured. The slightest of sound would cause an alarm, triggering an immediate mode of defence. While we struggle from the…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Class Loader

The main task of Class-loader is taking the class and loading it into the memory area. Class-loader basically handles three tasks.

In this step, the class loader takes the class and puts it into the memory area. Before doing that, the class loader reads some information of the class such as its’ immediate parent, instance variable information, whether it’s an enum, interface, or a class and whether it has a fully qualified name.

If all these criteria are met, then the class is loaded to the memory area. Then, the JVM creates an object from the class type.

Class type is a special data type in Java. At the very first time when a class is loaded to the memory, JVM creates an object from the class type. Then, it assigns the object created from the loaded class to that class and stores it in the Heap.

Creating an object from the class type for a particular class happens only once. If we again create an object from the loaded class, it wouldn’t be class type but object type.

Three additional tasks take place in the linking step.

JVM has a byte code verifier. Its purpose is to check if a particular class is safe to execute or not. This verifier checks whether the class comes from a valid compiler and whether it has correct formatting or structure.

If any of these are not satisfied, then JVM throws a runtime exception called verifier exception. This is why Java is said to be safe to execute in any environment. After being verified, the class can move to the preparation process.

2. Preparation

In this step, instead of using values assigned by the developer, JVM assigns default values for all the static and instance variables used in the program. This is a programmer-friendly method followed by Java.

3. Resolution

JVM doesn’t understand the symbols, names, and words we use in our codes. So, before reaching the machine level, JVM makes the code easier to understand by replacing those symbolic links with direct links.

To do that, JVM takes specific memory locations reserved by the objects and variables used in the code and replaces the codes with the particular memory locations.

In this final step, actual values are assigned to static and instance variables that were previously assigned with default values in the preparation step. The static block in the code is also executed in this step.

## In JVM, we can perform loading, linking, and initialization steps sequentially or parallelly. But there’s a rule to follow. It is, every class must be initialized before the active use of the particular class.

# According to the rules of JVM, a class must go through the initialization phase before any of its’ active uses. Apart from these six scenarios, all the others are considered passive use of the class. Those classes do not require going through the initialization phase in order to use them in a specific instance.

There are four ways Java can initialize a class.

# As we discussed earlier, in the initialization process, JVM assigns initial values to the variables. But how object values are assigned in the above four methods is different from each other.

# For example, if we use the clone() method to create objects, it gets its’ value from the parent object(source object) to assign as the initial value.

# Likewise, using the new keyword and using the getInstance() method will assign their initial values by going through the initialization process.

Consider the above code. When the compiler compiles this code, it creates a method called init() for each constructor. Developers can’t access this method. The components that get inside the init() depends on the class structure.

init() for Student() constructor

init( ){

}

## If we don’t specify which constructor to work using this this(), then init() has code to invoke parent class default constructor’s init() method.

init() for Student(int id) constructor

init( ){

}

#So the above examples show that what’s inside the init() depends on the class structure.

There’s a rule stating that there can be only codes of three types inside the init() method.

Let’s take a look at the following code.

Sample java code

# When the above class is compiled, the compiler creates a new method for this class that doesn’t have any name. Developers don’t have access to this method.

# Compiler puts all the static and instance variables in the code inside that method.

( ){

String name; int id;

}

# If a class does not have any static or variables, this method won’t exist.

Add a comment

Related posts:

GRATITUDE ATTITUDE

God is crystal clear on this issue: we are all called to be thanks-givers. Who? You! When? In all circumstances! Why? Because it’s God’s will for you! Whatever else you may be confused about…

Hope Poem

This walk may be barefoot, bleeding All alone, or even shiver to death by freezing Dazed by too many cross roads that are trying to muddle your toe from moving There are creatures awaits your stumble…

my live

im living at indonesia. “my live” is published by zahida mardhiyah.