Skip to content

Commit 44578ef

Browse files
committed
JDK 21: added JEP 445 examples
1 parent c14b9b4 commit 44578ef

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class InstanceMainMethod {
2+
void main() {
3+
System.out.println("Hello world from an instance main method o/");
4+
}
5+
}

java-21/README.md

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ To run each example use: `java --enable-preview --source 21 <FileName.java>`
2727
* the main change is remove the support for record pattern in header of an enhanced for loop
2828
* Pattern matching for `switch`
2929
* main changes from last JEPs:
30-
* removed parenthesized patterns (`switch int i when (i > 0)`)
30+
* removed parenthesized patterns (`case int i when (i > 0)`)
3131
* allow qualified enum constants as case constants
3232
* exhaustiveness and compatibility:
3333
* compiler will only require exhaustiveness if the switch uses any pattern, null label or if selector expression isn't from a legacy type (char, byte, short, int, Character, Byte, Short, Integer, String or a enum)
@@ -105,9 +105,53 @@ To run each example use: `java --enable-preview --source 21 <FileName.java>`
105105
* `unmodifiableSequencedCollection`
106106
* `unmodifiableSequencedSet`
107107
* `unmodifiableSequencedMap`
108-
* APIs:
109-
* improve `Thread.sleep(millis, nanos)` to actually perform sleep with sub-millisecond time
110-
* [`java.net.http.HttpClient` is now `AutoCloseable`](https://jdk.java.net/21/release-notes#JDK-8267140) and new methods were added to close/shutdown the connection pool.
108+
* Unnamed classes and instance main methods
109+
* facilitate the writing of first programm for students without needing to know another features designed for large programs
110+
* unnamed class:
111+
* any method declared in a source file without an enclosed class will be considered to be member of an unnamed top-level class
112+
* the compiler requires an unnamed method to have a valid main method to be launched
113+
* unnamed class is always final and cannot implement or extends any class other than `Object`
114+
* is equivalent to the following usage of anonymous class declaration:
115+
```java
116+
new Object() {
117+
void main() {}
118+
}.main();
119+
```
120+
* as it doesn't have a name, so we can use its static methods like `ClassName.staticMethodName()`
121+
* it still will have the impliticy `this` reference
122+
* it can have:
123+
* instance and static methods
124+
* instance and static fields
125+
* instance and static initializers
126+
* the default modifers are the same (package access and instance membership)
127+
* the main difference is will only have an impliticy default zero-paramater constructor
128+
* `Class.isSynthetic` method returns true
129+
* this JEPs introduces changes to how Java programs are launched:
130+
* instance main method:
131+
* the class must have a non-private zero-paramater constructor
132+
* the main method doesn't need to be static, public nor have parameter
133+
* example:
134+
```java
135+
class HelloWord {
136+
void main() {
137+
System.out.println("Hello World!");
138+
}
139+
}
140+
```
141+
* allowing unnamed class:
142+
```java
143+
void main() {
144+
System.out.println("Hello World!");
145+
}
146+
```
147+
* order to select a main method (all must be non-private and it prioritize the methods in current class before the superclass):
148+
* `static void main(String[])`
149+
* `static void main()`
150+
* `void main(String[])`
151+
* `void main()`
152+
* APIs:
153+
* improve `Thread.sleep(millis, nanos)` to actually perform sleep with sub-millisecond time
154+
* [`java.net.http.Http Client` is now `AutoCloseable`](https://jdk.java.net/21/release-notes#JDK-8267140) and new methods were added to close/shutdown the connection pool.
111155
112156
113157
## Links
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
void main() {
3+
System.out.println("Hello world from a unnamed class with a main method o/");
4+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
static String staticField = "can have static field";
2+
static String privateStaticField = "can have private static field";
3+
4+
String instanceField = "can have instance field";
5+
private String privateInstanceField = "can have private instance field";
6+
7+
static void staticMethod() {
8+
System.out.println("can have static method");
9+
}
10+
11+
private static void privateStaticMethod() {
12+
System.out.println("can use any modifier");
13+
}
14+
15+
static void instanceMethod() {
16+
System.out.println("can have any instance method");
17+
}
18+
19+
20+
void main() {
21+
System.out.println("must always have a valid main method to launch");
22+
23+
System.out.println("static field: " + staticField);
24+
System.out.println("instance field: " + instanceField);
25+
26+
staticMethod();
27+
this.instanceMethod();
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
static {
2+
System.out.println("static initializer");
3+
}
4+
5+
{
6+
System.out.println("instance initializer");
7+
}
8+
9+
void main() {
10+
System.out.println("instance main method");
11+
}

0 commit comments

Comments
 (0)