Eclipse默認主程序入口
Public class 的main函數
1 package com.study; 2 3 public class Test 4 { 5 6 7 public static void main(String[] args) 8 { 9 A a=new A(); 10 a.print(); 11 } 12 } 13 14 class A 15 { 16 public void print() 17 { 18 System.out.println("welcome !"); 19 } 20 }
運行結果

將主函數移至非公共類中
1 package com.study; 2 3 public class Test 4 { 5 6 7 8 } 9 10 class A 11 { 12 public static void main(String[] args) 13 { 14 A a=new A(); 15 a.print(); 16 } 17 public void print() 18 { 19 System.out.println("welcome !"); 20 } 21 }
運行結果

更改運行的main class后
默認

更改后

運行結果

結論
main函數不一定要放在public class里面,也可以放在其他class里面,但是IDE默認運行時去public class里尋找main函數。
建議
建議還是把main函數放在public Class里面。
