forked from akash-coded/C133-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticNestedClasses.java
48 lines (40 loc) · 1.75 KB
/
StaticNestedClasses.java
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
class OuterClass1 {
static int x = 10;
private int y = 20;
static class StaticNestedClass {
static int staticNestedX = 1000;
int staticNestedY = 2000;
void nonStaticFun() {
System.out.print("Non-static method of static nested class:: ");
System.out.print(x + " "); // Can only access static members of the outer class
System.out.println(staticNestedX + " " + staticNestedY); // Can access both static and non-static members of
// the outer class
}
static void staticFun() {
System.out.print("Static method of static nested class:: ");
System.out.print(x + " "); // Can only access static members of the outer class
System.out.println(staticNestedX); // Can only access static members of the nested class
}
}
public void outerFun() {
System.out.print("Instance Method of Outer Class:: ");
System.out.println(x + " " + y);
}
}
public class StaticNestedClasses {
public static void main(String[] args) {
OuterClass1 outer = new OuterClass1();
outer.outerFun();
// Accessing static fields of nested static class
System.out.print("\nMain method:: ");
System.out.println(OuterClass1.StaticNestedClass.staticNestedX);
// Calling non-static function of nested static class
System.out.println();
OuterClass1.StaticNestedClass staticNested = new OuterClass1.StaticNestedClass();
staticNested.nonStaticFun();
// Calling static function of nested static class
System.out.println();
staticNested.staticFun();
OuterClass1.StaticNestedClass.staticFun();
}
}