Friday, February 13, 2015

Got Crazy with nested classes :)

This time I tried enclosing 23 levels of sub classes with a combination of static and plain nested subclasses and guess what!!! It worked for me. Guess My demo of nested sub classes in java is world class rofl

Step 1: created a class like the following

package com.oz.core.NestedClasses;

public class OuterClass {

public static class NestedClass {

public class SubNestedClass {
public class Level4 {
public class Level5 {
public class Level6 {
public class Level7 {
public class Level8 {
public class Level9 {
public class Level10 {
public class Level11 {
public class Level12 {
public class Level13 {
public class Level14 {
public class Level15 {
public class Level16 {
public class Level17 {
public class Level18 {
public class Level19 {
public class Level20 {
public class Level21 {
public class Level22 {
public class Level {
Level() {
System.out
.println("Road To Eldorado WHOOO...HOOOO");
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}

}

Step 2: instantiated the classes Lol

package com.oz.core.NestedClasses;

import com.oz.core.NestedClasses.OuterClass.NestedClass.SubNestedClass.Level4.Level5;

public class Main {
public static void main(String args[]) {
OuterClass outCls = new OuterClass();
OuterClass.NestedClass nesCls=new OuterClass.NestedClass();
OuterClass.NestedClass.SubNestedClass subNestedClass=
nesCls.new SubNestedClass();
OuterClass.NestedClass.SubNestedClass subNestedClass2=
new OuterClass.NestedClass().new SubNestedClass();

OuterClass.NestedClass.SubNestedClass.Level4 lvl4= subNestedClass2. new Level4();
OuterClass.NestedClass.SubNestedClass.Level4.Level5.Level6.Level7.Level8.Level9.Level10
.Level11.Level12.Level13.Level14.Level15.Level16
.Level17.Level18.Level19.Level20.Level21.Level22
.Level level = lvl4.new Level5().new Level6().new Level7().new Level8().new Level9().new Level10()
.new Level11().new Level12().new Level13().new Level14().new Level15().new Level16().new Level17().new Level18()
.new Level19().new Level20().new Level21().new Level22().new Level();
}
}

Step 3: ran the main program to see the output on the console ☺

Console Output:

Road To Eldorado WHOOO...HOOOO