File tree Expand file tree Collapse file tree
main/java/com/baeldung/nestedclass
test/java/com/baeldung/nestedclass Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .nestedclass ;
2+
3+ abstract class SimpleAbstractClass {
4+ abstract void run ();
5+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments