In Java, you can hide the main constructor of a class by making it privateand then accessing it using the method public staticinside this class:
public final class Foo {
public static final Foo constructorA() {
return new Foo(someData);
}
private final Data someData;
private Foo(final Data someData) {
Objects.requireNonNull(someData);
this.someData = someData;
}
}
How can one achieve with Kotlin without splitting the class into an interface publicand an implementation private? Creating a constructor privateleads to the fact that it is not accessible from outside the class, not even from the same file.
source
share