首页 > 技术文章 > JAVA(5)之关于main函数的默认放置位置

quxiangjia 2020-01-16 22:00 原文

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

推荐阅读