Nested Classes in Java
This commit is contained in:
parent
79a4d133c3
commit
138633863c
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
public class Enclosing {
|
||||||
|
|
||||||
|
public static class Nested {
|
||||||
|
|
||||||
|
public void test() {
|
||||||
|
System.out.println("Calling test...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
public class NewEnclosing {
|
||||||
|
|
||||||
|
void run() {
|
||||||
|
class Local {
|
||||||
|
|
||||||
|
void run() {
|
||||||
|
System.out.println("Welcome to Baeldung!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Local local = new Local();
|
||||||
|
local.run();
|
||||||
|
}
|
||||||
|
}
|
11
core-java/src/main/java/com/baeldung/nestedclass/Outer.java
Normal file
11
core-java/src/main/java/com/baeldung/nestedclass/Outer.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
public class Outer {
|
||||||
|
|
||||||
|
public class Inner {
|
||||||
|
|
||||||
|
public void test() {
|
||||||
|
System.out.println("Calling test...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
abstract class SimpleAbstractClass {
|
||||||
|
abstract void run();
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class AnonymousInnerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenRunAnonymousClass_thenCorrect() {
|
||||||
|
SimpleAbstractClass simpleAbstractClass = new SimpleAbstractClass() {
|
||||||
|
void run() {
|
||||||
|
System.out.println("Running Anonymous Class...");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
simpleAbstractClass.run();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class LocalClassTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTestingLocalClass_thenCorrect() {
|
||||||
|
NewEnclosing newEnclosing = new NewEnclosing();
|
||||||
|
newEnclosing.run();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class NestedClassTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenInstantiatingStaticNestedClass_thenCorrect() {
|
||||||
|
Enclosing.Nested nested = new Enclosing.Nested();
|
||||||
|
nested.test();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user