Add a code snippet for anonymous classes (BAEL-2833) (#6690)
* Add a code snippet about anonymous classes (BAEL-2833) Add a code snippet about anonymous classes (BAEL-2833) Create module core-java-2 and move 'anonymous' package there * Adjust the pom of core-java-8-2 module * Adjust unit test
This commit is contained in:
parent
c18f588f28
commit
34c0cbe8e3
6
core-java-8-2/README.md
Normal file
6
core-java-8-2/README.md
Normal file
@ -0,0 +1,6 @@
|
||||
=========
|
||||
|
||||
## Core Java 8 Cookbooks and Examples (part 2)
|
||||
|
||||
### Relevant Articles:
|
||||
- [Anonymous Classes in Java](http://www.baeldung.com/)
|
43
core-java-8-2/pom.xml
Normal file
43
core-java-8-2/pom.xml
Normal file
@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-8-2</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-8-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
</project>
|
15
core-java-8-2/src/main/java/com/baeldung/anonymous/Book.java
Normal file
15
core-java-8-2/src/main/java/com/baeldung/anonymous/Book.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
64
core-java-8-2/src/main/java/com/baeldung/anonymous/Main.java
Normal file
64
core-java-8-2/src/main/java/com/baeldung/anonymous/Main.java
Normal file
@ -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<Runnable> actions = new ArrayList<Runnable>(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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
18
core-java-8-2/src/test/java/com/baeldung/UnitTest.java
Normal file
18
core-java-8-2/src/test/java/com/baeldung/UnitTest.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
2
pom.xml
2
pom.xml
@ -380,6 +380,7 @@
|
||||
<!-- <module>core-java-11</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 -->
|
||||
<!-- <module>core-java-12</module> --> <!-- We haven't upgraded to java 12. Fixing in BAEL-10841 -->
|
||||
<module>core-java-8</module>
|
||||
<module>core-java-8-2</module>
|
||||
<!--<module>core-java-9</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
|
||||
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->
|
||||
<module>core-java-arrays</module>
|
||||
@ -1035,6 +1036,7 @@
|
||||
<!-- <module>core-java-10</module> --> <!-- We haven't upgraded to java 10. Fixing in BAEL-10841 -->
|
||||
<!-- <module>core-java-11</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 -->
|
||||
<module>core-java-8</module>
|
||||
<module>core-java-8-2</module>
|
||||
<!--<module>core-java-9</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
|
||||
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->
|
||||
<module>core-java-arrays</module>
|
||||
|
Loading…
x
Reference in New Issue
Block a user