为什么Java中的主程序被放入类中? [英] Why the main program in Java is put into a class?

查看:104
本文介绍了为什么Java中的主程序被放入类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么main方法必须放在类中?我理解OOP的主要想法,但我不能得到为什么主程序在类中定义。这样的类会在某处实例化吗?我的意思是没有类外面的代码。

Why does the main method have to be put into a class? I understand the main ideas of OOP but I cannot get why the main program is defined within a class. Will such a class instantiated somewhere? I mean there is no code outside the class. What is a reason to define a class and never use objects of this class?

推荐答案

Java虚拟机

The Java Virtual Machine (JVM) has to start the application somewhere. As Java does not have a concept of "things outside of a class" the method that is called by the JVM has to be in a class. And because it is static, no instance of that class is created yet.

这篇关于为什么Java中的主程序被放入类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆