Skip to content

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

Closed

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

Activity

  1. datho7561 commented on Jun 8, 2026

    @datho7561
    Contributor

    Seems like an upstream bug in eclipse.jdt.core, so I'll file it there (and leave this one open as well to track progress of the fix being integrated here)

  2. datho7561 commented on Jun 8, 2026

    @datho7561
    Contributor

    After taking a look at the spec, I think that this behaviour from the Java compiler is okay. The compiler forms an implicit class named "main" when the class is called "main.java", and this overlaps with the main method.

  3. UnderHear commented on Jun 9, 2026

    @UnderHear
    Author

    I did some research and found that this issue seems to exist across all Java versions.

    I understand why this warning exists. The rule in eclipse.jdt.core is probably intended to catch mistakes like this:

    public class User {
    
        public void User() {   // The developer probably meant to write a constructor
        }
    }

    In this case, the method name is the same as the class name, but it has a return type, so it is not actually a constructor.

    However, public static void main(String[] args) clearly cannot be a constructor, because constructors cannot have a return type and cannot be static. Despite that, it still triggers this inspection rule when the method name matches the class name.

    So I think this inspection rule has a false-positive case and is not smart enough in this scenario.

  4. UnderHear commented on Jun 9, 2026

    @UnderHear
    Author

    I did some research and found that this issue seems to exist across all Java versions.

    I understand why this warning exists. The rule in eclipse.jdt.core is probably intended to catch mistakes like this:

    public class User {

    public void User() {   // The developer probably meant to write a constructor
    }
    

    }
    In this case, the method name is the same as the class name, but it has a return type, so it is not actually a constructor.

    However, public static void main(String[] args) clearly cannot be a constructor, because constructors cannot have a return type and cannot be static. Despite that, it still triggers this inspection rule when the method name matches the class name.

    So I think this inspection rule has a false-positive case and is not smart enough in this scenario.

    Image Image
  5. datho7561 commented on Jun 10, 2026

    @datho7561
    Contributor

    I think it makes sense to not warn about it in this case, given the spec also states that implicit class declarations cannot have a constructor.

  6. datho7561 commented on Jun 23, 2026

    @datho7561
    Contributor

    @UnderHear try out the latest prerelease version of the extension; the fix should be released from my testing

  7. UnderHear commented on Jun 24, 2026

    @UnderHear
    Author
    Image

    Yep, the warning is gone now. Awesome work — the console is finally at peace. 🎉

    Image

    thank u for resolving my OCD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions