main函數(shù)的作用只是作為一個入口
使Java虛擬機(jī)知道從哪里執(zhí)行
一旦程序啟動main便成為類中一個普通的方法
不調(diào)用不執(zhí)行
這時再new main函數(shù)所在類便不會執(zhí)行main函數(shù),只會執(zhí)行構(gòu)造方法
例1
class kk { public kk() { System.out.println("First"); } public static void main(String[] args) { new kk(); }}
例2
class kk extends Thread { public static void main(String[] args) { Thread threadNew1 = new kk();// 創(chuàng)建線程 Thread threadNew2 = new kk();// 創(chuàng)建線程 threadNew1.start();// 調(diào)用線程的strt()方法啟動線程 threadNew2.start(); while(true) { String threadName = Thread.currentThread().getName(); System.out.println(threadName); } } @Override public void run() { while(true) { String threadName = Thread.currentThread().getName(); System.out.println(threadName); } }}
本段代碼new main函數(shù)所在的類,但是并沒有使線程越來越多,因?yàn)閙ain函數(shù)僅執(zhí)行了一次