Invalid constructor name error in Apex

When deploying apex classes, a user may run into an odd error: Error: Compile Error: Invalid constructor name.

How to solve Invalid constructor name

Upon initial inspection, this error may be misleading. The class in question contains a working constructor or a new method was added that is not a constructor.

What gives?

Normally, this error indicates that a new method was added without adding the return type.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class Foo {

    private String bar;

    public Foo(String bar) {
        this.bar = bar;
    }

    public newMethod() {
        // logic
    }
}

To fix this, simply add the return type to the method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class Foo {

    private String bar;

    public Foo(String bar) {
        this.bar = bar;
    }

    // added void return type
    public void newMethod() {
        // logic
    }
}

Why does this error happen?

When a return type is omitted, the method is interpreted as a constructor because constructors do not contain return types.