From 339552010ebf0aace41af80b59d0f6ea64cb2429 Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Wed, 29 Nov 2017 15:28:08 +0200 Subject: [PATCH] enhance nested classes --- .../com/baeldung/nestedclass/Enclosing.java | 11 ------- .../java/com/baeldung/nestedclass/Outer.java | 11 ------- .../nestedclass/SimpleAbstractClass.java | 5 --- ...mousInnerTest.java => AnonymousInner.java} | 8 +++-- .../com/baeldung/nestedclass/Enclosing.java | 21 ++++++++++++ .../baeldung/nestedclass/InnerClassTest.java | 13 -------- .../baeldung/nestedclass/LocalClassTest.java | 12 ------- .../baeldung/nestedclass/NestedClassTest.java | 12 ------- .../baeldung/nestedclass/NewEnclosing.java | 11 +++++-- .../com/baeldung/nestedclass/NewOuter.java | 30 +++++++++++++++++ .../baeldung/nestedclass/NewOuterTest.java | 32 ------------------- .../java/com/baeldung/nestedclass/Outer.java | 20 ++++++++++++ 12 files changed, 86 insertions(+), 100 deletions(-) delete mode 100644 core-java/src/main/java/com/baeldung/nestedclass/Enclosing.java delete mode 100644 core-java/src/main/java/com/baeldung/nestedclass/Outer.java delete mode 100644 core-java/src/main/java/com/baeldung/nestedclass/SimpleAbstractClass.java rename core-java/src/test/java/com/baeldung/nestedclass/{AnonymousInnerTest.java => AnonymousInner.java} (72%) create mode 100644 core-java/src/test/java/com/baeldung/nestedclass/Enclosing.java delete mode 100644 core-java/src/test/java/com/baeldung/nestedclass/InnerClassTest.java delete mode 100644 core-java/src/test/java/com/baeldung/nestedclass/LocalClassTest.java delete mode 100644 core-java/src/test/java/com/baeldung/nestedclass/NestedClassTest.java rename core-java/src/{main => test}/java/com/baeldung/nestedclass/NewEnclosing.java (59%) create mode 100644 core-java/src/test/java/com/baeldung/nestedclass/NewOuter.java delete mode 100644 core-java/src/test/java/com/baeldung/nestedclass/NewOuterTest.java create mode 100644 core-java/src/test/java/com/baeldung/nestedclass/Outer.java diff --git a/core-java/src/main/java/com/baeldung/nestedclass/Enclosing.java b/core-java/src/main/java/com/baeldung/nestedclass/Enclosing.java deleted file mode 100644 index a9911538b0..0000000000 --- a/core-java/src/main/java/com/baeldung/nestedclass/Enclosing.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.nestedclass; - -public class Enclosing { - - public static class Nested { - - public void test() { - System.out.println("Calling test..."); - } - } -} diff --git a/core-java/src/main/java/com/baeldung/nestedclass/Outer.java b/core-java/src/main/java/com/baeldung/nestedclass/Outer.java deleted file mode 100644 index ebd6d27293..0000000000 --- a/core-java/src/main/java/com/baeldung/nestedclass/Outer.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.nestedclass; - -public class Outer { - - public class Inner { - - public void test() { - System.out.println("Calling test..."); - } - } -} diff --git a/core-java/src/main/java/com/baeldung/nestedclass/SimpleAbstractClass.java b/core-java/src/main/java/com/baeldung/nestedclass/SimpleAbstractClass.java deleted file mode 100644 index 586e2d12b4..0000000000 --- a/core-java/src/main/java/com/baeldung/nestedclass/SimpleAbstractClass.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.baeldung.nestedclass; - -abstract class SimpleAbstractClass { - abstract void run(); -} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/nestedclass/AnonymousInnerTest.java b/core-java/src/test/java/com/baeldung/nestedclass/AnonymousInner.java similarity index 72% rename from core-java/src/test/java/com/baeldung/nestedclass/AnonymousInnerTest.java rename to core-java/src/test/java/com/baeldung/nestedclass/AnonymousInner.java index 394c0bb57a..9fa8ee9cd5 100644 --- a/core-java/src/test/java/com/baeldung/nestedclass/AnonymousInnerTest.java +++ b/core-java/src/test/java/com/baeldung/nestedclass/AnonymousInner.java @@ -2,10 +2,14 @@ package com.baeldung.nestedclass; import org.junit.Test; -public class AnonymousInnerTest { +abstract class SimpleAbstractClass { + abstract void run(); +} + +public class AnonymousInner { @Test - public void whenRunAnonymousClass_thenCorrect() { + public void run() { SimpleAbstractClass simpleAbstractClass = new SimpleAbstractClass() { void run() { System.out.println("Running Anonymous Class..."); diff --git a/core-java/src/test/java/com/baeldung/nestedclass/Enclosing.java b/core-java/src/test/java/com/baeldung/nestedclass/Enclosing.java new file mode 100644 index 0000000000..3db33cde9b --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nestedclass/Enclosing.java @@ -0,0 +1,21 @@ +package com.baeldung.nestedclass; + +import org.junit.Test; + +public class Enclosing { + + private static int x = 1; + + public static class StaticNested { + + private void run() { + System.out.println("x = " + x); + } + } + + @Test + public void test() { + Enclosing.StaticNested nested = new Enclosing.StaticNested(); + nested.run(); + } +} diff --git a/core-java/src/test/java/com/baeldung/nestedclass/InnerClassTest.java b/core-java/src/test/java/com/baeldung/nestedclass/InnerClassTest.java deleted file mode 100644 index e9cb119ac2..0000000000 --- a/core-java/src/test/java/com/baeldung/nestedclass/InnerClassTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.nestedclass; - -import org.junit.Test; - -public class InnerClassTest { - - @Test - public void givenInnerClassWhenInstantiating_thenCorrect() { - Outer outer = new Outer(); - Outer.Inner inner = outer.new Inner(); - inner.test(); - } -} diff --git a/core-java/src/test/java/com/baeldung/nestedclass/LocalClassTest.java b/core-java/src/test/java/com/baeldung/nestedclass/LocalClassTest.java deleted file mode 100644 index dad19161ad..0000000000 --- a/core-java/src/test/java/com/baeldung/nestedclass/LocalClassTest.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.baeldung.nestedclass; - -import org.junit.Test; - -public class LocalClassTest { - - @Test - public void whenTestingLocalClass_thenCorrect() { - NewEnclosing newEnclosing = new NewEnclosing(); - newEnclosing.run(); - } -} diff --git a/core-java/src/test/java/com/baeldung/nestedclass/NestedClassTest.java b/core-java/src/test/java/com/baeldung/nestedclass/NestedClassTest.java deleted file mode 100644 index 16c883689a..0000000000 --- a/core-java/src/test/java/com/baeldung/nestedclass/NestedClassTest.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.baeldung.nestedclass; - -import org.junit.Test; - -public class NestedClassTest { - - @Test - public void whenInstantiatingStaticNestedClass_thenCorrect() { - Enclosing.Nested nested = new Enclosing.Nested(); - nested.test(); - } -} diff --git a/core-java/src/main/java/com/baeldung/nestedclass/NewEnclosing.java b/core-java/src/test/java/com/baeldung/nestedclass/NewEnclosing.java similarity index 59% rename from core-java/src/main/java/com/baeldung/nestedclass/NewEnclosing.java rename to core-java/src/test/java/com/baeldung/nestedclass/NewEnclosing.java index c7e04e8600..deeb72de0c 100644 --- a/core-java/src/main/java/com/baeldung/nestedclass/NewEnclosing.java +++ b/core-java/src/test/java/com/baeldung/nestedclass/NewEnclosing.java @@ -1,10 +1,11 @@ package com.baeldung.nestedclass; +import org.junit.Test; + public class NewEnclosing { - void run() { + private void run() { class Local { - void run() { System.out.println("Welcome to Baeldung!"); } @@ -12,4 +13,10 @@ public class NewEnclosing { Local local = new Local(); local.run(); } + + @Test + public void test() { + NewEnclosing newEnclosing = new NewEnclosing(); + newEnclosing.run(); + } } \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/nestedclass/NewOuter.java b/core-java/src/test/java/com/baeldung/nestedclass/NewOuter.java new file mode 100644 index 0000000000..a3a723b30e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nestedclass/NewOuter.java @@ -0,0 +1,30 @@ +package com.baeldung.nestedclass; + +import org.junit.Test; + +public class NewOuter { + + int a = 1; + static int b = 2; + + public class InnerClass { + int a = 3; + static final int b = 4; + + public void run() { + System.out.println("a = " + a); + System.out.println("b = " + b); + System.out.println("NewOuterTest.this.a = " + NewOuter.this.a); + System.out.println("NewOuterTest.b = " + NewOuter.b); + System.out.println("NewOuterTest.this.b = " + NewOuter.this.b); + } + } + + @Test + public void test() { + NewOuter outer = new NewOuter(); + NewOuter.InnerClass inner = outer.new InnerClass(); + inner.run(); + + } +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/nestedclass/NewOuterTest.java b/core-java/src/test/java/com/baeldung/nestedclass/NewOuterTest.java deleted file mode 100644 index e883687d33..0000000000 --- a/core-java/src/test/java/com/baeldung/nestedclass/NewOuterTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.nestedclass; - -import static org.junit.Assert.assertEquals; -import org.junit.Test; - -public class NewOuterTest { - - int a = 1; - static int b = 2; - - public class InnerClass { - int a = 3; - static final int b = 4; - - @Test - public void whenShadowing_thenCorrect() { - assertEquals(3, a); - assertEquals(4, b); - assertEquals(1, NewOuterTest.this.a); - assertEquals(2, NewOuterTest.b); - assertEquals(2, NewOuterTest.this.b); - } - } - - @Test - public void shadowingTest() { - NewOuterTest outer = new NewOuterTest(); - NewOuterTest.InnerClass inner = outer.new InnerClass(); - inner.whenShadowing_thenCorrect(); - - } -} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/nestedclass/Outer.java b/core-java/src/test/java/com/baeldung/nestedclass/Outer.java new file mode 100644 index 0000000000..d5e46670c9 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nestedclass/Outer.java @@ -0,0 +1,20 @@ +package com.baeldung.nestedclass; + +import org.junit.Test; + +public class Outer { + + public class Inner { + + public void run() { + System.out.println("Calling test..."); + } + } + + @Test + public void test() { + Outer outer = new Outer(); + Outer.Inner inner = outer.new Inner(); + inner.run(); + } +}