-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOuter.java
More file actions
32 lines (23 loc) · 1.21 KB
/
Copy pathOuter.java
File metadata and controls
32 lines (23 loc) · 1.21 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
package Nested;
public class Outer {
int x = 10; // instance variable of outer class
static int y = 20; // static variable of outer class
// non-static inner class
/** if you create non-static inner class then inner class associated with the instance of outer class and you can create object of inner class only through the object of outer class */
class Inner {
// instance variable of inner class
private int z = 30;
void display() {
System.out.println("Value of x: " + x); // ✔ can access instance variable of outer class
System.out.println("Value of y: " + y); // ✔ can access static variable of outer class
}
}
// static nested class
/* if you create static nested class then inner class is not associated with the instance of outer class and you can create object of static nested class without creating object of outer class */
static class StaticNested {
void display() {
// System.out.println("Value of x: " + x); // ❌ cannot access instance variable of outer class
System.out.println("Value of y: " + y); // ✔ can access static variable of outer class
}
}
}