可以:public final class HelloWorld
{
private static HelloWorld instance = null;
private HelloWorld()
{
}
public static HelloWorld getInstance()
{
if (instance == null)
{
instance = new HelloWorld();
}
return instance;
}
public void sayHello()
{
System.out.println("hello world!!");
}
public static void sayHello2()
{
System.out.println("hello world 222 !!");
}
static class Test
{
public static void main(String[] args) throws Exception
{
try
{
Class class1 = Class.forName("HelloWorld");
Constructor[] constructors = class1.getDeclaredConstructors();
AccessibleObject.setAccessible(constructors, true);
for (Constructor con : constructors)
{
if (con.isAccessible())
{
Object classObject = con.newInstance();
Method method = class1.getMethod("sayHello");
method.invoke(classObject);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
effective java中第三条就说了这种方式。还强调枚举才是单例的王道。