小言_互联网的博客

Java 中的main方法原理介绍。

222人阅读  评论(0)

//深入了解main方法

// 解释main方法的形式为什么是这样写的?

// public static void main(String[] args)

// 问题1:是谁调用main方法?

// java虚拟机调用main方法,所以main的访问权限是public

// 问题2:为什么是static 存储方式?

// java虚拟机没有创建main方法这个实例,只是单纯的调用,所以使用static 。

// 这个方法为什么需要接受string 数组?

// 你在使用命令的时候为什么要是这种形式:command option args1 args2 ...

// 也就是在执行这个程序之前传入的参数。

// 如下编译运行程序所示:


  
  1. public class main {
  2. public static void main( String[] args) {
  3. // 遍历args字符串数组。
  4. for(int i= 0;i<args. length;i++)
  5. {
  6. System. out. println(args[i]);
  7. }
  8. }
  9. }

运行结果:

// 这又有什么用呢?

// 对于shell用户或是Linux用户来说非常的重要也非常的方便快捷。



 

// 如果要使用main类中的方法,如果这个方法不是静态的,我们还是需要实例化。


 


  
  1. public class main {
  2. void fun( )
  3. {
  4. System. out. println( "this is fun method");
  5. }
  6. public static void main( String[] args) {
  7. // 实例化main类。
  8. new main(). fun();
  9. }
  10. }

运行结果:

 

 


转载:https://blog.csdn.net/weixin_53064820/article/details/128441308
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场