Search code examples
javacheckstyle

Java checkstyle - constructor definition in wrong order


I have a class that looks like this:

public final class OrderedSetList<T extends Comparable<? super T>> implements OrderedSet<T> {

    // Constructor definition in wrong order checkstyle error next line
    public OrderedSetList() {      
        // Initializations
    }
}

Can anyone tell me why there is a "Constructor definition in wrong order" error in my constructor?

It's an assignment and we have our own checkstyle configs and any checkstyle error is not allowed.

I appreciated your help.


Solution

  • The checkstyle rule is making sure you are following the code conventions for the order of your declarations:

    The parts of a class or interface declaration should appear in the following order:
    Class (static) variables. First the public class variables, then the protected, then package level (no access modifier), and then the private.
    Instance variables. First the public class variables, then the protected, then package level (no access modifier), and then the private.
    Constructors
    Methods

    It wants the constructor to be the first method.