Search code examples
javacoding-styleconvention

Java Convention to Return List of Own Instances?


I have a question about Java Coding Style.

I coded:

public class Pattern {
    public Pattern(...) { ... }

    public static List<Pattern> generatePatterns( ... ) { ... }
}

Since class instances do not contain static functions I think there is no problem, however, it feels awkward to statically generate a list of own instances from inside...?

What do you think?


Solution

  • In general, static can be seen as abnormality in good OO design. Meaning: you are careful about using it, as it leads to direct coupling of your classes, kills polymorphism and leads to hard-to-test code.

    But sometimes, static methods are ok; for example to have specific "generator" methods. Think of Optionals.of() or the various methods in Collections.

    From that perspective, your example seems to be fine. But we can't tell you whether your implementation of this method is as such that making it static is beneficial in the long term.