1.What’s the difference between A and B? A.class Test{public void test(){ synchronized(this) { ... }}} B.class Test{ public synchronized void test() { ... } }
2. Why the init() method must be private? Please provide an example to explain it. class Test{public Test(){ init();} private void init(){ …}} 3. Find problems in this code snippet: class Test{public static void executeQuery( String connectionName, String statementText) throws SQLException{ Connection connection = null; try { //Get a connection from connection pool connection = manager.getConnection(connectionName); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(statementText); for (int i = 0; resultSet.next(); i++) { ... } resultSet.close(); statement.close(); return ; } catch (SQLException e) { throw(new SQLException(statementText)); }}}
2. Why the init() method must be private? Please provide an example to explain it. class Test{public Test(){ init();} private void init(){ …}} 3. Find problems in this code snippet: class Test{public static void executeQuery( String connectionName, String statementText) throws SQLException{ Connection connection = null; try { //Get a connection from connection pool connection = manager.getConnection(connectionName); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(statementText); for (int i = 0; resultSet.next(); i++) { ... } resultSet.close(); statement.close(); return ; } catch (SQLException e) { throw(new SQLException(statementText)); }}}
2, init如果public的话,可以被外界调用,相当可以重新初始化了3,最明显的就是connection没关闭,然后关闭最好写在finally里面,那个return没必要
例子:
abstract class Glyph{
abstract void draw();
Glyph(){
System.out.println("Glyph() before draw()");
draw();
System.out.println("Glyph() after draw()");
}
}class RoundGlyph extends Glyph{
int radius=1;
RoundGlyph(int r){
radius=r;
System.out.println("RoundGlyph.RoundGlyph(),radius="+radius);
}
void draw(){
System.out.println("RoundGlyph.draw(),radius= "+radius);
}
}public class PolyConstructors{
public static void main(String[]args){
new RoundGlyph(5);
}
}java编程思想里专门讲到了这一问题