|
|
Re: How to cancel the creation of an instance
|
Posted: May 3, 2004 4:29 AM
|
|
A Constructor should (can?) never return null.
If you really need to do it using a constructor, throw an exception, if the argument is null.
Class MyClass { private MyClass (Object myParameter) throws ArgumentIsNullException { if (myParameter == null) throw new ArgumentIsNullException(...); } class ArgumentIsNullException extends Exception { ... } }
Otherwise I would recommend this way to do it:
Class MyClass { /** No Instance can be created directly */ private MyClass (Object myParameter) { }
/** This method does the trick. It returns null if a null value was passed as argument. */ public static MyClass createInstanceOfMyClass (Object myParameter) { return myParameter == null? null : new MyClass(myParameter); } }
|
|