//深入了解main方法
// 解释main方法的形式为什么是这样写的?
// public static void main(String[] args)
// 问题1:是谁调用main方法?
// java虚拟机调用main方法,所以main的访问权限是public
// 问题2:为什么是static 存储方式?
// java虚拟机没有创建main方法这个实例,只是单纯的调用,所以使用static 。
// 这个方法为什么需要接受string 数组?
// 你在使用命令的时候为什么要是这种形式:command option args1 args2 ...
// 也就是在执行这个程序之前传入的参数。
// 如下编译运行程序所示:
  
   - 
    
     
    
    
     
      public 
      class 
      main {
     
    
 
   - 
    
     
    
    
         
     
    
 
   - 
    
     
    
    
                     
      public 
      static 
      void 
      main(
      String[] args) {
     
    
 
   - 
    
     
    
    
                                     
     
    
 
   - 
    
     
    
    
                                     
      // 遍历args字符串数组。
     
    
 
   - 
    
     
    
    
                                     
      for(int i=
      0;i<args.
      length;i++)
     
    
 
   - 
    
     
    
    
     
                                      {
     
    
 
   - 
    
     
    
    
                                                     
      System.
      out.
      println(args[i]);
     
    
 
   - 
    
     
    
    
     
                                      }
     
    
 
   - 
    
     
    
    
     
                      }
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
  
 
运行结果:

// 这又有什么用呢?
// 对于shell用户或是Linux用户来说非常的重要也非常的方便快捷。
  
// 如果要使用main类中的方法,如果这个方法不是静态的,我们还是需要实例化。
  
  
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      public 
      class 
      main {
     
    
 
   - 
    
     
    
    
                     
      void 
      fun(
      )
     
    
 
   - 
    
     
    
    
     
                      {
     
    
 
   - 
    
     
    
    
                                     
      System.
      out.
      println(
      "this is fun method");
     
    
 
   - 
    
     
    
    
     
                      }
     
    
 
   - 
    
     
    
    
                     
      public 
      static 
      void 
      main(
      String[] args) {
     
    
 
   - 
    
     
    
    
                                     
      // 实例化main类。
     
    
 
   - 
    
     
    
    
                                    
      new 
      main().
      fun();
     
    
 
   - 
    
     
    
    
     
                      }
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
  
 
运行结果:

转载:https://blog.csdn.net/weixin_53064820/article/details/128441308
查看评论