Why java main function is declared as static type?
- 一个暂且说的过去的解释
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?的更多相关文章
- 07 java main方法
1.问题:Java main方法为什么是 public static void main(String[] args)??? 序号 场景 编译 运行 解释 1 public修改为private pr ...
- java main方法
1.问题:Java main方法为什么是 public static void main(String[] args)??? 序号 场景 编译 运行 解释 1 public修改为private pr ...
- Function接口 – Java8中java.util.function包下的函数式接口
Introduction to Functional Interfaces – A concept recreated in Java 8 Any java developer around the ...
- Main function
Main function A program shall contain a global function named main, which is the designated start of ...
- JAVA8的java.util.function包 @FunctionalInterface
1 函数式接口java.util.function https://www.cnblogs.com/CobwebSong/p/9593313.html 2 JAVA8的java.util.functi ...
- JAVA8的java.util.function包
一 概述 name type description Consumer Consumer< T > 接收T对象,不返回值 Predicate Predicate< T > 接收 ...
- Java 8 Function 函数接口
这篇文章属于 Java 8 教程(LTS)系列教程 在 Java 8 中,Function 接口是一个函数接口,它位于包 java.util.function 下. Function 接口中定义了一个 ...
- IAR for msp430 MDK中 warning: #223-D: function "xxx" declared implicitly 解决方法
今天在EINT的范例里添加了一个函数,即eint.c中添加了一个datawrite()的函数,并在主函数main.c中调用,编译便警告 warning: #223-D: function " ...
- java main()静态方法
java main()方法是静态的.意味着不需要new(),就在内存中存在.而且是属于类的,但是对象还是可以调用的. 若干个包含这个静态属性和方法的对象引用都可以指向这个内存区域.这个内存区域发生改变 ...
随机推荐
- HDU 4786 Fibonacci Tree
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) P ...
- jmeter接口自动化,你敢想,我敢玩
飞测说:大家好,我是黑夜小怪,今天我又来了分享了.最近用jmeter比较多,做过自动化测试的都知道,我们脚本和数据维护是你十分头疼的事情,刚好黑夜小怪我最近接触到一个项目的接口测试,今天我们一起分享下 ...
- 多媒体编程基础之RGB和YUV
一.概念 1.什么是RGB? 对一种颜色进行编码的方法统称为“颜色空间”或“色域”.用最简单的话说,世界上任何一种颜色的“颜色空间”都可定义成一个固定的数字或变量.RGB(红.绿.蓝)只是众多颜色空间 ...
- JDK源码分析之集合03LinkedList
一.前言 LinkedList是双向列表,实现方式是使用链表的方式保存元素:除了第一个和最后一个元素外,每一个节点都包含一个指向前一个和指向后一个节点的指针和元素的值.其特点是插入删除效率高,而随机访 ...
- kubernetes学习笔记1
安装篇 Downloading Kubernetes You can either build a release from sources or download a pre-built relea ...
- js控制台调试
在web编程的过程中js代码出现错误,可以通过console.log()将相关信息输入到控制台进行调试. 清空控制台右击选 Clear console 菜单,或者输入 clear() 都行 控制台相关 ...
- java服务器
WebLogic BEA公司开发的(被Oracle收购了)收费的 支持JavaEE所有的规范(ejb servlet/jsp规范) java mysql(oracle) 2.WebSphe ...
- LoadRunner性能测试指挥中心Controller 《第四篇》
一.设计场景 1.设计性能测试场景 Controller有两个视图:设计(Design)视图和运行(Run)视图.启动Controller,在Controller主界面里,我们可以看到这两个视图. 1 ...
- TP-Link访问策略
参考资料: http://service.tp-link.com.cn/detail_article_111.html
- Linux下yum安装MPlayer 或 LVC视频播放器
添加第三方源 su -c 'rpm -ivh http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noar ...