KT.UTILITY_CLASS_WITH_PUBLIC_CONSTRUCTOR

Utility class with public constructor or not final

A class which only contains utility variables and functions with no concrete implementation can be refactored into an object or a class with a non-public constructor. Furthermore, this rule reports utility classes which are not final.

Noncompliant Code

Copy
class UtilityClassViolation {

    // public constructor here
    constructor() {
        // ...
    }

    companion object {
        val i = 0
    }
}

open class UtilityClassViolation private constructor() {

    // ...
}

Compliant Code

Copy
class UtilityClass {

    private constructor() {
        // ...
    }

    companion object {
        val i = 0
    }
}
object UtilityClass {

    val i = 0
}

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html