当然可以,构造函数除了在new 的时候被调用,与成员函数没有其他的区别。

解决方案 »

  1.   

    public class Demo1203_1
    {
    /**/
    Demo1203_1()
    {
    System.out.println("Hello!");
    prt();
    }
    public static void prt()
    {
    System.out.println("World!");
    }

    public static void main(String[] args) 
    {
    Demo1203_1 p=new Demo1203_1();
    }
    }
    result:
    ---------- Run Java Program ----------
    Hello!
    World!Output completed (1 sec consumed) - Normal Termination
      

  2.   

    //: Temp.java
    public class Temp {
      String name;
      Temp(String name) {
        System.out.println("In constructor.");
        setName(name);  //调用setName()给name覆值
      }
      public void setName(String name) {
        this.name=name;
      }
      public void getName() {
        System.out.println(name);
      }
      public static void main(String[] args) {
        Temp t = new Temp("Rock");
        t.getName();
      }
    }输出结果:
    In constructor.
    Rock
      

  3.   

    构造函数调用前,一定的保证初始话的完成,如果在构造函数调用前没明确初始赋值,系统自动对变量赋值,int, lang,char,byte,short 为0,boolean 为false. flaot be 0.0f. double be 0.0d.
      

  4.   

    Thinking in java里说得很清楚!自己多上机实践一下!