diff --git a/core-java-8-2/README.md b/core-java-8-2/README.md new file mode 100644 index 0000000000..e2b12e8819 --- /dev/null +++ b/core-java-8-2/README.md @@ -0,0 +1,6 @@ +========= + +## Core Java 8 Cookbooks and Examples (part 2) + +### Relevant Articles: +- [Anonymous Classes in Java](http://www.baeldung.com/) diff --git a/core-java-8-2/pom.xml b/core-java-8-2/pom.xml new file mode 100644 index 0000000000..7035f12fb7 --- /dev/null +++ b/core-java-8-2/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + com.baeldung + core-java-8-2 + 0.1.0-SNAPSHOT + core-java-8-2 + jar + + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + UTF-8 + 1.8 + 1.8 + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + diff --git a/core-java-8-2/src/main/java/com/baeldung/anonymous/Book.java b/core-java-8-2/src/main/java/com/baeldung/anonymous/Book.java new file mode 100644 index 0000000000..964156e8e4 --- /dev/null +++ b/core-java-8-2/src/main/java/com/baeldung/anonymous/Book.java @@ -0,0 +1,15 @@ +package com.baeldung.anonymous; + +public class Book { + + final String title; + + public Book(String title) { + this.title = title; + } + + public String description() { + return "Title: " + title; + } + +} diff --git a/core-java-8-2/src/main/java/com/baeldung/anonymous/Main.java b/core-java-8-2/src/main/java/com/baeldung/anonymous/Main.java new file mode 100644 index 0000000000..d4eed69567 --- /dev/null +++ b/core-java-8-2/src/main/java/com/baeldung/anonymous/Main.java @@ -0,0 +1,64 @@ +package com.baeldung.anonymous; + +import java.util.ArrayList; +import java.util.List; + +/** + * Code snippet that illustrates the usage of anonymous classes. + * + * Note that use of Runnable instances in this example does not demonstrate their + * common use. + * + * @author A. Shcherbakov + * + */ +public class Main { + + public static void main(String[] args) { + final List actions = new ArrayList(2); + + Runnable action = new Runnable() { + @Override + public void run() { + System.out.println("Hello from runnable."); + } + + }; + actions.add(action); + + Book book = new Book("Design Patterns") { + @Override + public String description() { + return "Famous GoF book."; + } + }; + + System.out.println(String.format("Title: %s, description: %s", book.title, book.description())); + + actions.add(new Runnable() { + @Override + public void run() { + System.out.println("Hello from runnable #2."); + + } + }); + + int count = 1; + + Runnable action2 = new Runnable() { + static final int x = 0; + // static int y = 0; + + @Override + public void run() { + System.out.println(String.format("Runnable with captured variables: count = %s, x = %s", count, x)); + } + }; + actions.add(action2); + + for (Runnable a : actions) { + a.run(); + } + } + +} diff --git a/core-java-8-2/src/test/java/com/baeldung/UnitTest.java b/core-java-8-2/src/test/java/com/baeldung/UnitTest.java new file mode 100644 index 0000000000..9757901f52 --- /dev/null +++ b/core-java-8-2/src/test/java/com/baeldung/UnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class UnitTest { + /** + * Stub test + */ + @Test + public void givenPreconditions_whenCondition_shouldResult() { + assertTrue(true); + } +} diff --git a/pom.xml b/pom.xml index 4cc7360fac..7fb9ca351f 100644 --- a/pom.xml +++ b/pom.xml @@ -380,6 +380,7 @@ core-java-8 + core-java-8-2 core-java-arrays @@ -1035,6 +1036,7 @@ core-java-8 + core-java-8-2 core-java-arrays