// Should you make a constructor $private$? This questions pops up a lot // in CS100: /* Opinion by DIS: * At a CS100 level, make your constructors $public$ or default visibility. * A $private$ constructor will hide the ability to create an object, and since * you need to practice making objects, $private$ defeats the purpose. */ /* Opinion by Niranjan, a former CS100 TA: * I saw a comment in the private_constructor.java file that said * that usually you don't make constructors private and it seemed * to give the impression that private constructors lead to errors. I was * worried by this because I have often seen private constructors being * used in industrial java code. They are useful to create special objects * of a class for use inside the class where the user should have no * knowledge of such special objects. They are also sometimes used for the * very purpose that you have shown in the example: to prevent the * use of a certain form of creating the object. Actually this is usually * done in C++ to prevent the compiler from creating a default constructor * that the user can use (i.e forcing the object to be always be created * using non-default constructors.) */ class Test { public int k = 1; private Test() {} } public class private_constructor { public static void main(String[] args) { System.out.println(new Test().k); } } /* Output: private_constructor.java:8: No constructor matching Test() found in class Test. System.out.println(new Test().k); ^ 1 error */