0% found this document useful (0 votes)
28 views8 pages

Static Inner Class Java Examples

The document provides examples of static inner classes in Java, demonstrating their ability to access static members of the outer class and the limitations regarding non-static members. It also highlights the use of static methods within static inner classes and the inability to define static inner classes within methods. Additionally, it discusses the use of multiple static inner classes and the correct way to access outer class members from a static inner class.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views8 pages

Static Inner Class Java Examples

The document provides examples of static inner classes in Java, demonstrating their ability to access static members of the outer class and the limitations regarding non-static members. It also highlights the use of static methods within static inner classes and the inability to define static inner classes within methods. Additionally, it discusses the use of multiple static inner classes and the correct way to access outer class members from a static inner class.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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]();

You might also like