Java: note that classes with entirely private constructors can't be subclassed

This commit is contained in:
Chris Smowton
2025-09-30 13:56:27 +01:00
parent fa8cbeeb44
commit f88daff45f
2 changed files with 24 additions and 2 deletions

View File

@@ -30,4 +30,18 @@ public class Test {
}
}
}
public static class AllPrivateConstructors {
Thread myThread;
private AllPrivateConstructors() {
myThread = new Thread("myThread");
// OK - class cannot be extended outside this file, and is not in fact extended
myThread.start();
}
public static AllPrivateConstructors create() {
return new AllPrivateConstructors();
}
}
}