刚才琢磨这个问题主要是在想,若是constructor抛出了exception,那么返回的object是什么一个状况呢?若是我这个object中有一些关键的资源没有初始化,好比说Database connection在建立的时候有可能抛出SQLException,会不会返回一个HALF-MADE的object呢?为了验证,写了以下代码,结论:若是constructor中抛出了exception,constructor不会返回任何object,甚至null也不会返回。(由于程序流程在执行到return以前就跳转到exception处理去了啊亲,固然不会return任何东西!)java
class Box { private final int number; public Box(int number) { if (number < 0) { throw new IllegalArgumentException("Negative number: " + number); } this.number = number; } public int getNumber() { return number; } } public class Foo { public static void main(String[] args) { Box box = new Box(999); try { box = new Box(-999); } catch (IllegalArgumentException e) { // ignored } System.out.println(box); } }
回到我刚才遇到的问题,通过这番思考以后,我改写了一下,使用了一个static factory method(固然,针对我这里的状况而言,其实使用constructor也是能够的)ui
// Data Source public class DatabaseConnection { // data source name private static final String DSNAME = "java:comp/env/jdbc/fooooooooooooo"; private Connection conn = null; private DatabaseConnection(Connection conn) { this.conn = conn; } /** * Construct a new instance of DatabaseConnection. * @return a new instance of DatabaseConnection. * */ public static DatabaseConnection newInstance() throws NamingException, SQLException { Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup(DSNAME); Connection conn = ds.getConnection(); return new DatabaseConnection(conn); } public Connection getConnection() { return conn; } public void close() throws SQLException { conn.close(); } }
我搜索了3篇有关constructor的问题,值得一看:this
一、Is it good practice to make the constructor throw an exception?spa
里面讨论了从constructor抛出exception是否是good practicecode
值得一提的是,里面还讨论了检查argument的正确性应该用exception仍是assertionblog
二、Is doing a lot in constructors bad?资源
三、How much code should one put in a constructors (Java)?get
2和3讨论了constructor应该作哪些工做,不该该作哪些工做,作多少工做合适,以及在什么状况下应该考虑使用Factory或者Builderit