1. 一个暂且说的过去的解释

   The method is static because otherwise there would be ambiguity: which constructor should be called? Especially if your class looks like this:

 public class JavaClass{
protected JavaClass(int x){}
public void main(String[] args){
}
}

  Should the JVM call new JavaClass(int)? What should it pass for x?

If not, should the JVM instantiate JavaClass without running any constructor method? I think it shouldn’t, because that will special-case your entire class – sometimes you have an instance that hasn’t been initialized, and you have to check for it in every method that could be called.

There are just too many edge cases and ambiguities for it to make sense for the JVM to have to instantiate a class before the entry point is called. That’s why main is static.

  个人对以上的理解就是为了避免无法确定该调用的构造函数而需将main函数设为先于类的构造而执行,故将其声明为static。

  参考:http://liunian.info/why-java-main-static.html

  2.

  static关键字,告知编译器main函数是一个静态函数。也就是说main函数中的代码是存储在静态存储区的,即当定义了类以后这段代码就已经存在了。如果main()方法没有使用static修饰符,那么编译不会出错,但是如果你试图执行该程序将会报错,提示main()方法不存在。因为包含main()的类并没有实例化(即没有这个类的对象),所以其main()方法也不会存。而使用static修饰符则表示该方法是静态的,不需要实例化即可使用。

Why java main function is declared as static type?的更多相关文章

  1. 07 java main方法

    1.问题:Java main方法为什么是  public static void main(String[] args)??? 序号 场景 编译 运行 解释 1 public修改为private pr ...

  2. java main方法

    1.问题:Java main方法为什么是  public static void main(String[] args)??? 序号 场景 编译 运行 解释 1 public修改为private pr ...

  3. Function接口 – Java8中java.util.function包下的函数式接口

    Introduction to Functional Interfaces – A concept recreated in Java 8 Any java developer around the ...

  4. Main function

    Main function A program shall contain a global function named main, which is the designated start of ...

  5. JAVA8的java.util.function包 @FunctionalInterface

    1 函数式接口java.util.function https://www.cnblogs.com/CobwebSong/p/9593313.html 2 JAVA8的java.util.functi ...

  6. JAVA8的java.util.function包

    一 概述 name type description Consumer Consumer< T > 接收T对象,不返回值 Predicate Predicate< T > 接收 ...

  7. Java 8 Function 函数接口

    这篇文章属于 Java 8 教程(LTS)系列教程 在 Java 8 中,Function 接口是一个函数接口,它位于包 java.util.function 下. Function 接口中定义了一个 ...

  8. IAR for msp430 MDK中 warning: #223-D: function "xxx" declared implicitly 解决方法

    今天在EINT的范例里添加了一个函数,即eint.c中添加了一个datawrite()的函数,并在主函数main.c中调用,编译便警告 warning: #223-D: function " ...

  9. java main()静态方法

    java main()方法是静态的.意味着不需要new(),就在内存中存在.而且是属于类的,但是对象还是可以调用的. 若干个包含这个静态属性和方法的对象引用都可以指向这个内存区域.这个内存区域发生改变 ...

随机推荐

  1. android View 自动 GONE 问题

    首先说一下 view visibility VISIBLE.INVISIBLE.GONE的区别: 可见(visible) XML文件:android:visibility="visible& ...

  2. 使用eclipse上传项目到开源中国代码托管Git@osc教程

    创建项目 安装EGit插件 没有的话看下图 生成公钥 注册git账号 新建git项目 添加公钥 建立本地代码库 本文章版权归博主所有,如需转载请私信.

  3. velocity freemarker比较

    相比较 FreeMarker 而言,Velocity 更加简单.轻量级,但它的功能却没有 FreeMarker 那么强大. 对于大部分的应用来说,使用 FreeMarker 比 Velocity 更简 ...

  4. def

    a = 97,A = 65,z = 122,Z = 90 小写字母比大写字母整形数值高,upper->lower相差32 把小写字母转化成大写字母,将小写字母 - 32 1. typedef 数 ...

  5. leetcode 110

    110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  6. 使用OrderBy对List<Person>集合排序

    string sortOrder = Request.QueryString["sortOrder"];   string sortField = Request.QueryStr ...

  7. jquery 入门之-------jquery 简介

    什么是jquery? Jquery是一个Javascript库,通过封装原生的Javascript函数得到一套定义好的方法.它可以用个少的代码完成更多更强大更复杂的功能,从而得到开发者的青睐. So! ...

  8. 在Eclipse中查看JDK类库的源代码

    http://blog.csdn.net/a81895898/article/details/8486802 在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 “window”-> ...

  9. POJ C++程序设计 编程题#3 编程作业—运算符重载

    编程题 #3 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 写一个二维数组 ...

  10. C#winform在textbox插入内容换行

    要让一个TextBox显示多行文本就得把它的Multiline属性设置为true,可是如果你是要把TextBox的Text属性设置多行文本时可能会遇到点麻烦,也许你会想到直接加一个换行符"\ ...