1.
Basic Static Inner Class Example
class Outer {
static int y = 10;
static class Inner {
void show() {
[Link]("Inside static inner class, y = " + y);
public static void main(String[] args) {
[Link] obj = new [Link]();
[Link]();
Output:
Inside static inner class, y = 10
👉 Static inner class can directly access static members of the outer class.
It cannot directly access non-static members.
✅ 2. Static Inner Class with Static Methods
class Outer {
static int data = 100;
static class Inner {
static void display() {
[Link]("Static method inside static inner class: data = " + data);
public static void main(String[] args) {
[Link](); // No need to create object
Output:
Static method inside static inner class: data = 100
👉 You can call a static method of a static inner class without creating an object.
✅ 3. Static Inner Class Accessing Outer Class Non-static Members (via Object)
class Outer {
int x = 50;
static int y = 100;
static class Inner {
void show() {
[Link]("Static y = " + y);
// Cannot directly access Outer.x
Outer outerObj = new Outer();
[Link]("Non-static x (via object) = " + outerObj.x);
public static void main(String[] args) {
[Link] obj = new [Link]();
[Link]();
Output:
Static y = 100
Non-static x (via object) = 50
👉 Static inner class can’t access non-static fields directly, but it can if you create an object of the
outer class.
✅ 4. Multiple Static Inner Classes
class Outer {
static class Inner1 {
void display1() {
[Link]("Inside Inner1");
static class Inner2 {
void display2() {
[Link]("Inside Inner2");
public static void main(String[] args) {
Outer.Inner1 i1 = new Outer.Inner1();
Outer.Inner2 i2 = new Outer.Inner2();
i1.display1();
i2.display2();
Output:
Inside Inner1
Inside Inner2
✅ 5. Static Inner Class inside Method (NOT Allowed)
⚠️Note: Local inner classes can be defined inside methods, but static inner classes cannot.
So this will give a compile error:
class Outer {
void f1() {
static class Inner { // ❌ Not allowed
✅ 6. Using Static Inner Class in Another Class
class Outer {
static int data = 200;
static class Inner {
void show() {
[Link]("Accessing from another class: data = " + data);
public class TestStaticInner {
public static void main(String[] args) {
[Link] obj = new [Link]();
[Link]();
1. Basic Static Inner Class Example
class Outer {
static int y = 10;
static class Inner {
void show() {
[Link]("Inside static inner class, y = " + y);
}
}
public static void main(String[] args) {
[Link] obj = new [Link]();
[Link]();
Output:
Inside static inner class, y = 10
👉 Static inner class can directly access static members of the outer class.
It cannot directly access non-static members.
✅ 2. Static Inner Class with Static Methods
class Outer {
static int data = 100;
static class Inner {
static void display() {
[Link]("Static method inside static inner class: data = " + data);
public static void main(String[] args) {
[Link](); // No need to create object
Output:
Static method inside static inner class: data = 100
👉 You can call a static method of a static inner class without creating an object.
✅ 3. Static Inner Class Accessing Outer Class Non-static Members (via Object)
class Outer {
int x = 50;
static int y = 100;
static class Inner {
void show() {
[Link]("Static y = " + y);
// Cannot directly access Outer.x
Outer outerObj = new Outer();
[Link]("Non-static x (via object) = " + outerObj.x);
public static void main(String[] args) {
[Link] obj = new [Link]();
[Link]();
Output:
Static y = 100
Non-static x (via object) = 50
👉 Static inner class can’t access non-static fields directly, but it can if you create an object of the
outer class.
✅ 4. Multiple Static Inner Classes
class Outer {
static class Inner1 {
void display1() {
[Link]("Inside Inner1");
}
}
static class Inner2 {
void display2() {
[Link]("Inside Inner2");
public static void main(String[] args) {
Outer.Inner1 i1 = new Outer.Inner1();
Outer.Inner2 i2 = new Outer.Inner2();
i1.display1();
i2.display2();
Output:
Inside Inner1
Inside Inner2
✅ 5. Static Inner Class inside Method (NOT Allowed)
⚠️Note: Local inner classes can be defined inside methods, but static inner classes cannot.
So this will give a compile error:
class Outer {
void f1() {
static class Inner { // ❌ Not allowed
✅ 6. Using Static Inner Class in Another Class
class Outer {
static int data = 200;
static class Inner {
void show() {
[Link]("Accessing from another class: data = " + data);
public class TestStaticInner {
public static void main(String[] args) {
[Link] obj = new [Link]();
[Link]();