Skip to content

Commit e3487cd

Browse files
committed
JDK 25: update JEPs notes and examples
1 parent 611c48a commit e3487cd

8 files changed

+174
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ A project to explore more about the new features from Java 8 through Java 21.
1212
## Resume by Version
1313

1414
* [Java 25](java-25/)
15+
* Stable Values (Preview)
16+
* Remove the 32-bit x86 Port
17+
* Module Import Declarations
18+
* Compact Source Files and Instance Main Methods
1519

1620
* [Java 24](java-24/) (Mar, 2025)
1721
* Generational Shenandoah (experimental)

java-24/ModuleImportDeclarationsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import module java.sql;
33
import module java.desktop;
44

5-
import java.util.*; // priori List
5+
import java.util.*; // prioritize List
66
import java.util.Date;
77

88
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* To run: `java ImplicityDeclaredClassMainMethodLauncher.java`
3+
*/
4+
void main() {
5+
System.out.println("Hello world from a implicity declared class with a main method o/");
6+
}
7+
8+
/**
9+
To see the generated class:
10+
- `javac ImplicityDeclaredClassMainMethodLauncher.java`
11+
- `javap ImplicityDeclaredClassMainMethodLauncher`
12+
13+
Output:
14+
15+
```
16+
Compiled from "ImplicityDeclaredClassMainMethodLauncher.java"
17+
final class ImplicityDeclaredClassMainMethodLauncher {
18+
ImplicityDeclaredClassMainMethodLauncher();
19+
void main();
20+
}
21+
```
22+
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
void main() {
3+
// java.io.IO.readln
4+
String name = IO.readln("Enter your name: ");
5+
6+
// java.io.IO.println
7+
IO.println("Hello " + name + "!");
8+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* To run: `java ImplicityDeclaredClassWithAllowedMembers.java`
3+
*/
4+
5+
static String staticField = "can have static field";
6+
static String privateStaticField = "can have private static field";
7+
8+
String instanceField = "can have instance field";
9+
private String privateInstanceField = "can have private instance field";
10+
11+
static void staticMethod() {
12+
System.out.println("can have static method");
13+
}
14+
15+
private static void privateStaticMethod() {
16+
System.out.println("can use any modifier");
17+
}
18+
19+
static void instanceMethod() {
20+
System.out.println("can have any instance method");
21+
}
22+
23+
void main() {
24+
System.out.println("must always have a valid main method to launch");
25+
26+
System.out.println("static field: " + staticField);
27+
System.out.println("instance field: " + instanceField);
28+
29+
staticMethod();
30+
this.instanceMethod();
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* To run: `java InstanceMainMethodLauncher.java`
3+
*/
4+
class InstanceMainMethodLauncher {
5+
void main() {
6+
System.out.println("Hello world from an instance main method o/");
7+
}
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import module java.base;
2+
import module java.sql;
3+
import module java.desktop;
4+
5+
import java.util.*; // prioritize List
6+
import java.util.Date;
7+
8+
/**
9+
* Run: `java ModuleImportDeclarationsTest.java`
10+
*/
11+
public class ModuleImportDeclarationsTest {
12+
public static void main(String[] args) {
13+
Date date = new Date();
14+
15+
List l = new ArrayList();
16+
}
17+
}

java-25/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,89 @@ To run each example use: `java --enable-preview --source 25 <FileName.java>`
66

77
* [502](https://openjdk.org/jeps/502) - Stable Values (Preview)
88
* [503](https://openjdk.org/jeps/503) - Remove the 32-bit x86 Port
9+
* [511](https://openjdk.org/jeps/511) - Module Import Declarations
10+
* [512](https://openjdk.org/jeps/512) - Compact Source Files and Instance Main Methods
11+
12+
## Features
13+
14+
* **Module Import Declarations**
15+
* promotion to standard without change
16+
* allow import all packages exported by a module
17+
* goal is simplify the learning and exploring of APIs without having to know its exactly package
18+
* when importing a module, it will automatically import all its exported packages and its transitive dependencies
19+
* import module `java.se` will import the entire Java SE API
20+
* syntax:
21+
* `import module [Module Name]`
22+
* `import module java.sql`
23+
* ambiguous import:
24+
* in case of two module exporting the same class name, we can import the class directly or its package
25+
* allow any type-import-on-demand declarations to shadow module import declarations
26+
*
27+
```java
28+
// module imports
29+
import module java.base; // java.util.Date
30+
import module java.sql; // java.sql.Date
31+
32+
// package imports
33+
import java.util.*; // every class used will shadow the class from module imports
34+
35+
// single-type imports
36+
import java.sql.Date; // will shadow all above imports
37+
```
38+
* **Compact Source Files and Instance Main Methods**
39+
* promotion to standard with minor changes
40+
* changed terminology from "simple source file" to "compact source file"
41+
* changed package of `IO` to `java.lang.IO`
42+
* facilitate the writing of first programm for students without needing to know another features designed for large programs
43+
* implicitly declared class:
44+
* any method declared in a source file without an enclosed class will be considered to be member of an implicitly declared class whose members are unenclosed fields and methods
45+
* implicitly declared class is always final and cannot implement or extends any class other than `Object`
46+
* the compiler requires an implicitly declared method to have a launchable main method
47+
* we cannot use javadoc tool to generate documation from a implicitly declared class (doesn't have a accessible API from other class)
48+
* is equivalent to the following usage of anonymous class declaration:
49+
```java
50+
new Object() {
51+
void main() {}
52+
}.main();
53+
```
54+
* as it doesn't have a name, so we cannot use its static methods using its class name (`ClassName.staticMethodName()`)
55+
* it still will have the impliticy `this` reference
56+
* it can have:
57+
* instance and static methods
58+
* instance and static fields
59+
* the default modifers are the same (package access and instance membership)
60+
* the main difference is will only have an impliticy default zero-paramater constructor and cannot have initializers (static nor instance)
61+
* implicitly class will automatically import all of the public top-level classes and interfaces from module `java.base`
62+
* `Class.isSynthetic` method returns true
63+
* launchable main method:
64+
* change to how Java programs are launched
65+
* instance main method:
66+
* the class must have a non-private zero-paramater constructor
67+
* the main method doesn't need to be static, public nor have parameter
68+
* example:
69+
```java
70+
class HelloWord {
71+
void main() {
72+
System.out.println("Hello World!");
73+
}
74+
}
75+
```
76+
* allowing implicitly declared class:
77+
```java
78+
void main() {
79+
System.out.println("Hello World!");
80+
}
81+
```
82+
* order to select a main method (must be non-private and it prioritize the methods in current class before the superclass):
83+
* `static void main(String[])`
84+
* `void main(String[])`
85+
* `static void main()`
86+
* `void main()`
87+
* `java.lang.IO`
88+
* new class created for convenience I/O
89+
* provides methods: `print(Object)`, `println(Object)`, `println()`, `readln(String)` and `readln()`
90+
* this class uses return from `System.console()` to print and read from the default input and output streams
91+
* we can use with: `IO.println("Hello World")`
992

1093
## Links
1194

0 commit comments

Comments
 (0)