Why can public inline functions call private constructors

Viewed 2921

I have a private constructor for my class and implemented invoke on the companion object for some kind of "generic constructor"

class Test private constructor(className: String) {
    companion object {
        // If I remove the internal it fails 
        internal inline operator fun <reified T> invoke(): Test {
            return Test(T::class.java.name) // why can I even call it? The constructor is private
        }
    }
}

and I can even have a public inline function that calls that generic constructor

public inline fun test() = Test<Any>() // Why can I call it, it is internal

shouldn't that mean that every invokation test() expands to Test(Any::class.java.name) even though that constructor is private?

So my questions are:

  • Why can that internal inline fun call a private constructor? (a public fun couldn't)
  • Why can that public inline fun call an internal function?
  • And why can I ultimately expose a private constructor in a public inline fun?
2 Answers
Related