Skip to content

Commit 34c0cbe

Browse files
veontomomaibin
authored andcommitted
Add a code snippet for anonymous classes (BAEL-2833) (eugenp#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
1 parent c18f588 commit 34c0cbe

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

core-java-8-2/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=========
2+
3+
## Core Java 8 Cookbooks and Examples (part 2)
4+
5+
### Relevant Articles:
6+
- [Anonymous Classes in Java](http://www.baeldung.com/)

core-java-8-2/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>core-java-8-2</artifactId>
8+
<version>0.1.0-SNAPSHOT</version>
9+
<name>core-java-8-2</name>
10+
<packaging>jar</packaging>
11+
12+
13+
<parent>
14+
<groupId>com.baeldung</groupId>
15+
<artifactId>parent-java</artifactId>
16+
<version>0.0.1-SNAPSHOT</version>
17+
<relativePath>../parent-java</relativePath>
18+
</parent>
19+
20+
<properties>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<maven.compiler.source>1.8</maven.compiler.source>
23+
<maven.compiler.target>1.8</maven.compiler.target>
24+
</properties>
25+
26+
<dependencies>
27+
</dependencies>
28+
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-compiler-plugin</artifactId>
34+
<version>${maven-compiler-plugin.version}</version>
35+
<configuration>
36+
<source>${maven.compiler.source}</source>
37+
<target>${maven.compiler.target}</target>
38+
</configuration>
39+
</plugin>
40+
</plugins>
41+
42+
</build>
43+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.anonymous;
2+
3+
public class Book {
4+
5+
final String title;
6+
7+
public Book(String title) {
8+
this.title = title;
9+
}
10+
11+
public String description() {
12+
return "Title: " + title;
13+
}
14+
15+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.baeldung.anonymous;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Code snippet that illustrates the usage of anonymous classes.
8+
*
9+
* Note that use of Runnable instances in this example does not demonstrate their
10+
* common use.
11+
*
12+
* @author A. Shcherbakov
13+
*
14+
*/
15+
public class Main {
16+
17+
public static void main(String[] args) {
18+
final List<Runnable> actions = new ArrayList<Runnable>(2);
19+
20+
Runnable action = new Runnable() {
21+
@Override
22+
public void run() {
23+
System.out.println("Hello from runnable.");
24+
}
25+
26+
};
27+
actions.add(action);
28+
29+
Book book = new Book("Design Patterns") {
30+
@Override
31+
public String description() {
32+
return "Famous GoF book.";
33+
}
34+
};
35+
36+
System.out.println(String.format("Title: %s, description: %s", book.title, book.description()));
37+
38+
actions.add(new Runnable() {
39+
@Override
40+
public void run() {
41+
System.out.println("Hello from runnable #2.");
42+
43+
}
44+
});
45+
46+
int count = 1;
47+
48+
Runnable action2 = new Runnable() {
49+
static final int x = 0;
50+
// static int y = 0;
51+
52+
@Override
53+
public void run() {
54+
System.out.println(String.format("Runnable with captured variables: count = %s, x = %s", count, x));
55+
}
56+
};
57+
actions.add(action2);
58+
59+
for (Runnable a : actions) {
60+
a.run();
61+
}
62+
}
63+
64+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
7+
/**
8+
* Unit test for simple App.
9+
*/
10+
public class UnitTest {
11+
/**
12+
* Stub test
13+
*/
14+
@Test
15+
public void givenPreconditions_whenCondition_shouldResult() {
16+
assertTrue(true);
17+
}
18+
}

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@
380380
<!-- <module>core-java-11</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 -->
381381
<!-- <module>core-java-12</module> --> <!-- We haven't upgraded to java 12. Fixing in BAEL-10841 -->
382382
<module>core-java-8</module>
383+
<module>core-java-8-2</module>
383384
<!--<module>core-java-9</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
384385
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->
385386
<module>core-java-arrays</module>
@@ -1035,6 +1036,7 @@
10351036
<!-- <module>core-java-10</module> --> <!-- We haven't upgraded to java 10. Fixing in BAEL-10841 -->
10361037
<!-- <module>core-java-11</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 -->
10371038
<module>core-java-8</module>
1039+
<module>core-java-8-2</module>
10381040
<!--<module>core-java-9</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
10391041
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->
10401042
<module>core-java-arrays</module>

0 commit comments

Comments
 (0)