当前位置:网站首页>为什么需要public static void main(String[ ] args)这个方法?
为什么需要public static void main(String[ ] args)这个方法?
2022-06-11 14:11:00 【小夕Coding】
为什么需要public static void main(String[ ] args)这个方法?
public static void main(String[ ]args)为Java程序的入口方法,JVM在运行程序时,会首先查找main()方法。其中,public 是权限修饰符,表明任何·类或者对象都可以访问这个方法,static表明main()方法是一个静态方法,即方法中的代码是存储在静态存储区的,只要类被加载后,就可以使用该方法而不需要通过实例化对象来访问,可以直接通过类名.main()直接访问,JVM在启动时就按照上述方法当签名(必须有public与static修饰,返回值为void,且方法当参数为字符串数组)来查找方法的入口地址,若能找到,就执行;找不到,则会报错。void表明方法没有返回值,main是JVM识别的特殊方法名,是程序的入口方法。字符串数组参数args为开发人员在命令行状态下与程序交互提供了一种手段。
因为main是程序等待入口方法,所以当程序运行时,第一个执行的方法就是main()方法。通常来讲,要执行一个类的方法,先必须实例化一个类的对象,然后通过对象来调用这个方法。但由于main是程序的入口方法,此时还没实例化对象,因此在编写main()方法时就要求不需要实例化对象就可以调用这个方法,鉴于此,main()方法需要被定义成public与static。下列给出了在调用main()方法时传递参数的方法。
public class T
边栏推荐
- Live800: several ways for intelligent customer service to improve customer experience
- 解决循环依赖BUG。Relying upon circular references is discouraged and they are prohibited by default.
- Pond (topology + priority queue)
- Tp6 whoops based exception takeover (beautiful interface)
- Recommended open source scheduling libraries worth learning
- .NET C#基础(6):命名空间 - 有名字的作用域
- In depth research and analysis report on global and Chinese liquid malt extract products market
- Three level classification display
- NoSQL之Redis配置与优化
- 基于Qt开发实现的任务管理器
猜你喜欢

Redis configuration and optimization of NoSQL

Implementation of VGA protocol based on FPGA

Why does each running Department display that the database already exists, delete the database, and then succeed? Each running department must delete the database, and re run the whole successfully

Two small things, feel the gap with the great God

The application of machine learning in database cardinality estimation

Unsealing easy QF PDA helps enterprises improve ERP management

Ali talked about the use of strategic mode in the project

使用cpolar远程办公(1)

全球手机市场衰退,连苹果也对iPhone14不抱过高期待

No delay / no delay live video instance effect cases
随机推荐
解决循环依赖BUG。Relying upon circular references is discouraged and they are prohibited by default.
Optimal dispatching (thermal power, wind and energy storage) (realized by Matlab)
mysql高级语句
Question bank and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
Current situation and future development trend of metal organic chemical vapor deposition (MOCVD) market in the world and China from 2022 to 2028
Airtest automated test
Alibaba Cloud 3 (Soaring Falcon) x86_ 64 (py3.7.8) system Yum source
C # set the cursor shape of forms and systems
Current situation and future development trend of global and Chinese transcranial magnetic stimulation coil Market from 2022 to 2028
Ali talked about the use of strategic mode in the project
CVPR 2022 | 神经辐射场几何编辑方法NeRF-Editing
Invalid bound statement (not found) error [resolved]
. Net C Foundation (6): namespace - scope with name
.NET C#基础(6):命名空间 - 有名字的作用域
2022 Gansu Province safety officer B certificate test question simulation test question bank and online simulation test
2022.2.27 library management system 3 - book borrowing and returning registration module
Current situation and future development trend of scaffold market in the world and China from 2022 to 2028
2022-2028 near infrared (NIR) analyzer Market Status and future development trend in the world and China
【公开课预告】:MXPlayer OTT音视频转码实践和优化
How to quickly compress the size of video?