ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

classLoading

Updated on August 23, 2012

Introduction

In java classes are loaded in either of the two way's

1.by explicit introduction.

2.by implicit reference.

♣In the first approach name of of the class to be loaded is explicitly told to the compiler .

for each application one class need to be explicitly introduced

syntax with example: java A(.class Name)↑

In the 2nd approach a class is implicitly loaded by the JRE whenever another class of reference is encountered for the first time during the execution.

most of the class are loaded using this approach.

simple example how JRE loads clas:

class A
{
public static void main(String args[])
{
B x=new B();// during the execution of A ,reference of class B is encountered.
System.out.println("Objected of class B is created and loaded ");
}

class B
{
//your code
}

At the time of class loading following sequence of operations are performed by the JRE

  1. static data member(if defined ) are created in the class area of memory block.
  2. static block(if defined) is executed.
  3. the operation which resulted in the class loading is performed.

example of class loading:

class A
{
static 
{
System.out.println("A is Initialized");
}
public A()
{
System.out.println("A is Instantiated ");
}
}
class B
{

static 
{
System.out.println("B is Initialized");
}
public static void display()
{
System.out.println("display() of B is executed");
}
}
class C
{
static int value;
static
{
value=10;
System.out.println("C is Initialized");
}
}
class D
{
static
{
System.out.println("D is Initialized");
}
public static void main(String args[])
{
System.out.println("Instantiating A ....");
A x=new A();
System.out.println("Invoking display() of B");
B.display();
System.out.println("referencing static data member of C ");
System.out.println(C.value);
System.out.println("Instantiating A again");
A y=new A();// creating new object of class A.
}
}

Output

D is initialized

instantiating A..........

A is initialized

A is instantiated

invoking display() of B

B is initialized

display() of B is executed

referencing static data member of C

c is Initialzied

10

Instantiating A again

A is Instantiated

static blocks and static methods have following limitations

  1. non-static data members of the class can't be referenced in a static block or methods.
  2. non-static methods can't be directly invoked by a static block or method.
  3. this and super keyword can't be used in a static block or methods.

how class load in java

Comments

    0 of 8192 characters used
    Post Comment

    • glassvisage profile image

      glassvisage 4 years ago from Northern California

      This is a great page with a lot of information on this topic - a great resource for anyone interesting in Java. It's great to have the graphics along with the text like you do. Thanks!

    • b. Malin profile image

      b. Malin 4 years ago

      A most Interesting and Informative Hub, Raushan. This was a topic I knew nothing about, so thank you for sharing.

      A warm welcome to Hub Pages, we are like a "Family" of Writers here. I now look forward to Following your Hubs.

    Click to Rate This Article