You are on page 1of 4

Nested or Inner Classes Class Loading:

Inner classes can be in a separate jar (Same class loader having both the jars in its classpath)

Program :
class innerTest {
static{
System.out.println("INNER OUTER/MAIN CLASS LOADED --> JAR 1 or 2");
}

class A {

{
System.out.println("INNER CLASS A LOADED --> JAR 1 or 2");
}
}

static class B{

{
System.out.println("INNER CLASS B LOADED --> JAR 1 or 2");
}
}
}

public class TestInner{

public static void main(String[] s){

System.out.println("#######MAIN#########");

innerTest obj=new innerTest();


innerTest.A obj1= new innerTest().new A();
innerTest.B obj2 =new innerTest.B();

}
Program :

class innerTest {
static{ System.out.println("INNER OUTER/MAIN CLASS LOADED --> JAR 1 or 2");}

class A {

{ System.out.println("INNER CLASS A LOADED --> JAR 1 or 2"); }


}

static class B{

{ System.out.println("INNER CLASS B LOADED --> JAR 1 or 2"); }


}

public class TestInner{

public static void main(String[] s){

System.out.println("#######MAIN#########");

innerTest obj=new innerTest();


innerTest.A obj1= new innerTest().new A();
innerTest.B obj2 =new innerTest.B();

Runnable r = new Runnable(){


public void run(){
System.out.println ("RUNNABLE from JAR1 or JAR2");
}
};

Thread t = new Thread(r);


t.start();

}
}
====================================================================
class A {

{
System.out.println("INNER CLASS A LOADED --> JAR 2");
Runnable r = new Runnable(){
public void run(){

System.out.println ("ANNONYMOUS RUNNABLE INNER CLASS from AN INNER


CLASS A -->JAR2");

};

Thread t = new Thread(r);


t.start();

}
}

SEALED=true flag will not behave this way as it will load all the classes of the same package from the
same jar

You might also like