-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNestedClasses.java
More file actions
50 lines (40 loc) · 1.67 KB
/
NestedClasses.java
File metadata and controls
50 lines (40 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Nested classes
// * Two Types
// * Non-Static Nested Class
// * Inner Class
// * Nested Class inside Outer Class Method
// * Anonymous Nested Class
// * static Nested Class
public class NestedClasses {
private static String msg = "Learning Nested Classes";
// Static Nested Class
static class NestedStaticClass {
public void printMessage() {
System.out.println("Message from nested static class: " + msg);
}
}
// Non-static Nested Class
class InnerClass {
public void display() {
System.out.println("Message from non-static nested class: " + msg);
}
}
public static void main(String[] args) {
// Create instance of nested static class
NestedStaticClass nestedStaticClass = new NestedStaticClass();
nestedStaticClass.printMessage();
// Create instance of non-static nested class
NestedClasses outer = new NestedClasses();
InnerClass inner = outer.new InnerClass();
inner.display();
// Accessing the inner class from outside the outer class
// In order to create instance of Inner class from outside the Outer class, we need an Outer class instance
// Let us create Outer class instance for creating non-static nested class
NestedClasses.InnerClass innerObject = new NestedClasses().new InnerClass();
innerObject.display();
// Accessing the inner class from outside the outer class using outer class instance
NestedClasses outerObject = new NestedClasses();
NestedClasses.InnerClass innerObject2 = outerObject.new InnerClass();
innerObject2.display();
}
}