The following program completes a Java program that does not require the main method by using the static initialization block method.
Public class Hello
{
Static {
System. out. println ("No MainJava say Hello, World ");
System. exit (0 );
}
}
I have also seen this technique used in the database metadata class to initialize the data pool.
Class SomeDBManager {
Private static InitialContext ic;
Private static DataSource ds;
Static {
If (ds = null ){
Try {
Ic = new InitialContext ();
Ds = (DataSource) ic. lookup ("java: comp/env/jdbc/snilispigwebapp ");
PrepareConnection ();
} Catch (NamingException ex ){
Ex. printStackTrace ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
}
Private static Connection getConn () throws SQLException {
Return ds. getConnection ();
}
Public static void prepareConnection () throws SQLException {
Connection conn = getConn ();
Conn. close ();
}
}