Skip to content

Commit 1386338

Browse files
author
Ahmed Tawila
committed
Nested Classes in Java
1 parent 79a4d13 commit 1386338

9 files changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.nestedclass;
2+
3+
public class Enclosing {
4+
5+
public static class Nested {
6+
7+
public void test() {
8+
System.out.println("Calling test...");
9+
}
10+
}
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.nestedclass;
2+
3+
public class NewEnclosing {
4+
5+
void run() {
6+
class Local {
7+
8+
void run() {
9+
System.out.println("Welcome to Baeldung!");
10+
}
11+
}
12+
Local local = new Local();
13+
local.run();
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.nestedclass;
2+
3+
public class Outer {
4+
5+
public class Inner {
6+
7+
public void test() {
8+
System.out.println("Calling test...");
9+
}
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.nestedclass;
2+
3+
abstract class SimpleAbstractClass {
4+
abstract void run();
5+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.nestedclass;
2+
3+
import org.junit.Test;
4+
5+
public class AnonymousInnerTest {
6+
7+
@Test
8+
public void whenRunAnonymousClass_thenCorrect() {
9+
SimpleAbstractClass simpleAbstractClass = new SimpleAbstractClass() {
10+
void run() {
11+
System.out.println("Running Anonymous Class...");
12+
}
13+
};
14+
simpleAbstractClass.run();
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.nestedclass;
2+
3+
import org.junit.Test;
4+
5+
public class InnerClassTest {
6+
7+
@Test
8+
public void givenInnerClassWhenInstantiating_thenCorrect() {
9+
Outer outer = new Outer();
10+
Outer.Inner inner = outer.new Inner();
11+
inner.test();
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.nestedclass;
2+
3+
import org.junit.Test;
4+
5+
public class LocalClassTest {
6+
7+
@Test
8+
public void whenTestingLocalClass_thenCorrect() {
9+
NewEnclosing newEnclosing = new NewEnclosing();
10+
newEnclosing.run();
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.nestedclass;
2+
3+
import org.junit.Test;
4+
5+
public class NestedClassTest {
6+
7+
@Test
8+
public void whenInstantiatingStaticNestedClass_thenCorrect() {
9+
Enclosing.Nested nested = new Enclosing.Nested();
10+
nested.test();
11+
}
12+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.nestedclass;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import org.junit.Test;
5+
6+
public class NewOuterTest {
7+
8+
int a = 1;
9+
static int b = 2;
10+
11+
public class InnerClass {
12+
int a = 3;
13+
static final int b = 4;
14+
15+
@Test
16+
public void whenShadowing_thenCorrect() {
17+
assertEquals(3, a);
18+
assertEquals(4, b);
19+
assertEquals(1, NewOuterTest.this.a);
20+
assertEquals(2, NewOuterTest.b);
21+
assertEquals(2, NewOuterTest.this.b);
22+
}
23+
}
24+
25+
@Test
26+
public void shadowingTest() {
27+
NewOuterTest outer = new NewOuterTest();
28+
NewOuterTest.InnerClass inner = outer.new InnerClass();
29+
inner.whenShadowing_thenCorrect();
30+
31+
}
32+
}

0 commit comments

Comments
 (0)