Skip to content

False warning “This method has a constructor name” for Java 25 compact source file named main.java #4424

@UnderHear

Description

@UnderHear

When using Java 25 compact source files, VS Code Java reports a warning on a valid top-level void main() method if the source file is named main.java.

Minimal repro:

void main() {
    UrlUtils.print123();
}

With another class in the same folder:

public final class UrlUtils {
    private UrlUtils() {
    }

    public static void print123() {
        System.out.println(123);
    }
}

Environment:

javac 25.0.2
openjdk version "25.0.2" 2026-01-20

Command-line compilation succeeds:

javac main.java UrlUtils.java

But VS Code Java reports this diagnostic on line 1:

This method has a constructor name
code: 67108974
source: Java

Expected behavior:

No warning should be reported. void main() is valid in a Java 25 compact source file.

Actual behavior:

The Java language server appears to infer an implicit class name from main.java, then treats void main() as a method whose name matches the class name and emits the old “constructor name” warning.

This seems like a false positive specific to Java 25 compact source files, especially when the filename is main.java.

Image

[{
"resource": "/C:/Users/25336/Desktop/project/bug/main.java",
"owner": "generated_diagnostic_collection_name#3",
"code": "67108974",
"severity": 4,
"message": "This method has a constructor name",
"source": "Java",
"startLineNumber": 1,
"startColumn": 6,
"endLineNumber": 1,
"endColumn": 12,
"modelVersionId": 20,
"origin": "extHost1"
}]

When the file name is no longer main.java, the warning will no longer appear.

Image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions