|
| 1 | +package NestedClass; |
| 2 | + |
| 3 | +class OuterClass { |
| 4 | + // static |
| 5 | + static int x_static; |
| 6 | + // nonstatic |
| 7 | + int x_nons; |
| 8 | + // private static |
| 9 | + private static int x; |
| 10 | + |
| 11 | + private int x_pri; |
| 12 | + |
| 13 | + OuterClass(int a, int b, int c, int d) { |
| 14 | + x_static = a; |
| 15 | + x_nons = b; |
| 16 | + x = c; |
| 17 | + x_pri = d; |
| 18 | + } |
| 19 | + void show(){ |
| 20 | + if (x_nons>20) { |
| 21 | + class Inner { |
| 22 | + |
| 23 | + void display() { |
| 24 | + System.out.println("static int" + x_static); |
| 25 | + System.out.println("Private static int" + x); |
| 26 | + System.out.println("Nonstatic Integer" + x_nons);// cannot be accessed directly |
| 27 | + System.out.println("Nonstatic Private integer" + x_pri);// cannot be accessed directly |
| 28 | + } |
| 29 | + } |
| 30 | + Inner obj = new Inner(); |
| 31 | + obj.display(); |
| 32 | + } |
| 33 | + else{ |
| 34 | + System.out.println("No inner class"); |
| 35 | + } |
| 36 | +} |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * NestedClass3 |
| 41 | + */ |
| 42 | +class NestedClass4 { |
| 43 | + |
| 44 | + public static void main(String[] args) { |
| 45 | + // accessing Inner class |
| 46 | + OuterClass obj1 = new OuterClass(10, 20, 30, 40); |
| 47 | + obj1.show(); |
| 48 | + OuterClass obj2 = new OuterClass(10, 50, 30, 40); |
| 49 | + obj2.show(); |
| 50 | + // Inner inner = new Inner(); |
| 51 | + //System.out.println(inner.display());will generate compilation error as inner class can be accessed only inside the block it belongs to |
| 52 | + |
| 53 | + } |
| 54 | +} |
0 commit comments