Java學習之代碼塊

package hello3;
public class TestCode {
           private String name;
           public TestCode(){
            System.out.println("構造方法執行了");
           }
           //構造代碼塊
           {
            System.out.println("構造代碼塊執行了");
           }
           //靜態代碼塊,加載TestCode.class時,直接執行了,所以首先執行,可是兩個對象時只執行一次,加載類時就執行過了
           static{
            System.out.println("靜態代碼塊執行了");
           }
           public  void jubu(){
          
            //局部代碼塊
            {
             System.out.println("局部代碼塊執行了");
            }
           }
           public static void main(String[] args){
            System.out.println("hello");
            TestCode tc1=new TestCode();
            tc1.jubu();
            TestCode tc2=new TestCode();
           }
}對象

運行結果;class

靜態代碼塊執行了
hello
構造代碼塊執行了
構造方法執行了
局部代碼塊執行了
構造代碼塊執行了
構造方法執行了方法

相關文章
相關標籤/搜索