The question is that "Is main() method is compulsory in Java?"

问题是“ main()方法在Java中是强制性的吗?”

  • Yes, we can write a java program without main() method but there is a condition if and only if java JDK version till JDK 5.

    是的,我们可以编写一个没有main()方法的Java程序,但是有一个条件,当且仅当Java JDK版本到JDK 5为止。

  • Till Java JDK 5 main() method was not compulsory to include in Java program.

    直到Java JDK 5 main()方法不是必须包含在Java程序中的。

  • If we don't write our code in the main() method or don't include main() method in our program then, in that case, we need to write our code under static block then only, in that case, we can execute our code normally as we do.

    如果我们不在main()方法中编写代码或在程序中不包含main()方法 ,则在这种情况下,我们只需要在静态块下编写代码,那么在这种情况下,我们可以像我们一样正常执行代码。

Example:

例:

// Java Program to demonstrate till Java JDK5 version
// without main() method is possible.
class WithoutMainMethod {static {int i = 2, j = 4, sum;
sum = i + j;
System.out.println("The sum of i and j is :" + sum);
System.out.println("This program is without main() valid till JDK 5 version");
}
}

Output

输出量

E:\Programs>javac WithoutMainMethod.java
E:\Programs>java WithoutMainMethod
The sum of i and j is : 6
This program is without main() valid till JDK 5 version

  • In the case of the static block is that static block executes before the main() method.

    在使用静态块的情况下,该静态块在main()方法之前执行。

  • Static block executes at the time of class loading.

    静态块在类加载时执行。

  • In the case of the main() method, our program starts executing from the main() method or in other words it is the starting point of the program execution.

    对于main()方法 ,我们的程序从main()方法开始执行,换句话说,它是程序执行的起点。

  • We can call the main() method directly without the creation of an object because it is static.

    我们可以直接调用main()方法 ,而无需创建对象,因为它是静态的。

  • Till Java JDK 5 main() method was not mandated, But from Java JDK 6 main() is mandatory and if we don't include main() method in our program then we will get RuntimeException "main method not found in the class".

    直到Java JDK 5 main()方法没有强制执行,但是从Java JDK 6 main()开始是强制性的,如果我们在程序中不包含main()方法 ,则将得到RuntimeException “在类中找不到main方法”

Example:

例:

// Program to demonstrate without main() method
// from Java JDK 6 version
class WithoutMain{int i=2 , j=4 , sum=0;
sum = i + j;
System.out.println("The sum of i and j is :" + sum);
System.out.println("This program without main() is not valid from JDK 6 version");
}

Output

输出量

E:\Programs>javac WithoutMain.java
E:\Programs>java WithoutMain
Error: Main method not found in class WithoutMain, please define the main method as:
public static void main(String[] args)

翻译自: https://www.includehelp.com/java/is-main-method-compulsory-in-java.aspx

Java中的main()方法是强制性的吗?相关推荐

  1. Java中的main()方法详解

    源文作者:leizhimin    源文链接:http://lavasoft.blog.51cto.com/62575/53263 源文作者版权申明: 版权声明:原创作品,允许转载,转载时请务必以超链 ...

  2. Java中的main方法

    2019独角兽企业重金招聘Python工程师标准>>> 在一个Java应用程序中,通常程序的入口是一个main方法,它被声明为公有静态方法,参数是一个字符串数组,返回值为Void类型 ...

  3. Java main方法_解释Java中的main方法,及其作用_一个java文件中可包含多个main方法

    public static void main(String[] args) {} 或者 public static void main(String args[]) {} main方法是我们学习Ja ...

  4. 漫画:程序员太难了!为什么 Java 中的 main 方法必须是它?

    作者 | 漫话编程 来源 | 漫话编程(ID:mhcoding) 在 Java 中,想必所有人都不会对 main 方法感到陌生,main 方法是 Java 应用程序的入口方法.程序运行时,要执行的第一 ...

  5. 关于java中的main方法的几种写法

    Java中的main入口方法一般的入门书上都说只有一种固定的写法,但实际上可以有以下几种变种的写法,没有什么实际用处,纯属娱乐. 1.main方法的一般写法 public class TestMain ...

  6. 详解Java中的main方法

    main方法格式定义 public: main方法启动时由JVM进行加载,public的可访问权限是最高的,所有需要声明为public; static: 方法的调用要么是通过对象,要么是通过类,而ma ...

  7. java方法重载和重载方法_我们可以在Java中重载main()方法吗?

    java方法重载和重载方法 The question is that "can we overload main() method in Java?" 问题是"我们可以在 ...

  8. 我们可以覆盖Java中的main()方法吗?

    The question is that "Can we override main() method in Java?" 问题是"我们可以覆盖Java中的main()方 ...

  9. java中main是什么_Java中的main()方法详解

    在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的方法有很大的不同,比如方法的名字必须是main,方法必须是 ...

  10. main的方法是Java_Java中的main()方法

    在Java中,main()方法是Java應用程序的入口方法,也就是說,程序在運行的時候,第一個執行的方法就是main()方法,這個方法和其他的方法有很大的不同,比如方法的名字必須是main,方法必須是 ...

最新文章

  1. NLP情感分析 动手学深度学习博客
  2. python用表达式解密密文_基于Python解密仿射密码
  3. 我的大学到研究生自学 Java 之路,过程艰辛,不放弃,保持热情,最终发现我是这样拿到大厂 offer 的!...
  4. Django-你想知道的都在这里
  5. 抢先看!Kubernetes v1.21 新特性一览
  6. tcp网络通信教程 java_基于java TCP网络通信的实例详解
  7. 傲娇Android二三事之诡诡异异的图片加载
  8. 黑暗之魂3设置无边窗口化
  9. python机器视觉车牌识别_车牌识别系统中的机器视觉技术
  10. 服务器主板测试工程师的项目经历,硬件测试简历项目经验怎么写
  11. sap erp 消息服务器,九慧信息|SAP_SAP ERP_SAP系统_SAP s/4hana
  12. 基于halcon的颜色识别---杜邦线识别
  13. 初识爬虫——爬虫与HTML介绍
  14. 中标麒麟操作系统安装MySQL5.7.22
  15. configure文件的生成
  16. chrome浏览器自动填充时背景色改变(-webkit-autofill)
  17. Two Teams Composing
  18. 详细设计说明书(GB8567——88)
  19. Power BI与PowerQuery、PowerPivot有什么关系?
  20. Android--控件的单位(px,pt,dp,sp)

热门文章

  1. java中自动装箱的问题
  2. oracle字段类型设计,Oracle字段类型设计与实际业务不符引发的问题
  3. 容大打印机ip修改工具_M1芯片版Mac无法连接打印机怎么办?
  4. linq查询不包含某个值的记录_MySQL行(记录)的详细操作
  5. 网页无法调用java9_JAVA 9 (内部类,异常,包)
  6. assertpythonraise_使用assertRaise测试异常消息
  7. xodo上的笔记不见了_一起来“终极笔记名场面批发市场”进货吗
  8. JS事件的捕获和冒泡阶段
  9. Mac 设置 NDK
  10. [3/21]Windows Server 2008时钟方面的改进展示