I need to call a private constructor of a nested c...
# getting-started
p
I need to call a private constructor of a nested class from a companion defined in the outer class.
h
I don't think that's possible
a
If you really want to call private constructor you have to use kotlin-allopen-plugin and use annotations. Or you can use Java for that.
Copy code
Constructor<Foo> constructor= (Constructor<Foo>) Foo.class.getDeclaredConstructors()[0];
constructor.setAccessible(true); 
obj = constructor.newInstance("foo");
But in you case if you want define an internal class in the parent class, I think, you should use
Copy code
private class Child()
and you can create Child class everywhere in your Parent class, but you can't create Child outside the Parent class, because it's private. But according for your code whatever you want it's hide constructor of the class. And you can use something like this.
Copy code
class Child private constructor () {
companion object {
        operator fun invoke(): Child {
            return Child()
        }
    }
}
class Parent() {
companion object {
        operator fun foo = Child()
    }
}