在构造函数里直接调用这个方法不行吗?public MyTest()
{
          System.out.println("you are a bad man");//(1)
          sayHello();
}

解决方案 »

  1.   

    不行,一定要等构造方法执行完了,才能执行sayHello()
      

  2.   

    public class MyTest
    {
    public MyTest()
    {
              System.out.println("you are a bad man");//(1)
    }static
    {
    System.out.println("hello");//(2)
    }public static void main(String[]args)
    {
                 new MyTest();
    }
    }
    运行结果:
    you are a bad man
      

  3.   

    但是我想的结果是
    you are a bad man
    hello
    而hello是自动帮我调用
      

  4.   

    public class MyTest
    {
    public MyTest()
    {
              System.out.println("you are a bad man");//(1)
    }static
    {
    System.out.println("hello");//(2)
    }public static void main(String[]args)
    {
                 new MyTest();
    }
    }静态代码段应该满足不了楼主要求`不知道Java有没有这个功能。。 等答案
      

  5.   

    public class MyTest
    {
    public MyTest()
    {
    System.out.println("you are a bad man");//(1)
    }public void sayHello()
    {
    System.out.println("hello");//(2)
    }
    public static MyTest getInstance()
    {
       MyTest test = new MyTest();
       test.sayHello();
       return test;
    }public static void main(String[]args)
    {
       MyTest mt = MyTest.getInstance();
    }
    }