JAVA(5)之關於main函數的默認放置位置


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里面。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 1.Spring-Boot 靜態文件和頁默認放置位置 ThinkPHP5 與ThinkPHP3.2公共函數放置位置 Spring boot 配置文件默認放置位置,和加載優先級 html DOM(CSS放置位置的問題) 關於HTML(九)--------css js放置位置和原因 left join on 和where中條件的放置位置 在idea中寫入的html以及css這些文件放置位置 關於