Skip to content

Commit de7d191

Browse files
klueverError Prone Team
authored andcommitted
Update IncorrectMainMethod documentation in light of JEP512.
PiperOrigin-RevId: 821681565
1 parent 09c78e0 commit de7d191

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

docs/bugpattern/IncorrectMainMethod.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
A `main` method must be `public`, `static`, and return `void` (see
2-
[JLS §12.1.4]).
1+
## Pre Java 25
2+
3+
Prior to Java 25, a `main` method must be `public`, `static`, and return `void`
4+
(see [JLS §12.1.4]).
35

46
For example, the following method is confusing, because it is an overload of a
57
valid `main` method (it has the same name and signature), but is not a valid
@@ -18,7 +20,25 @@ $ java T.java
1820
error: 'main' method is not declared 'public static'
1921
```
2022

21-
[JLS §12.1.4]: https://docs.oracle.com/javase/specs/jls/se11/html/jls-12.html#jls-12.1.4
23+
## Java 25 and later
24+
25+
For Java 25 and later, a `main` method must return `void` (see [JLS §12.1.4]).
26+
The `public` and `static` requirements have been dropped.
27+
28+
For example, the following method is confusing, because it is an overload of a
29+
valid `main` method (it has the same name and arguments), but does not return
30+
`void`:
31+
32+
```java
33+
class Test {
34+
public static int main(String[] args) {
35+
System.err.println("hello world");
36+
return 0;
37+
}
38+
}
39+
```
40+
41+
[JLS §12.1.4]: https://docs.oracle.com/javase/specs/jls/se25/html/jls-12.html#jls-12.1.4
2242

2343
TIP: If you're declaring a method that isn't intended to be used as the main
2444
method of your program, prefer to use a name other than `main`. It's confusing

0 commit comments

Comments
 (0)