如果主要方法在“非公共类”内部怎么办?的java文件? [英] What if main method is inside "non public class" of java file?

查看:359
本文介绍了如果主要方法在“非公共类”内部怎么办?的java文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个类的java文件,其中一个是公共的。如果main方法在非公共类中。我无法运行该java文件。这是为什么?并且没有编译错误。如果是这样,我怎么能使用那个main方法?

I have a java file containing more than one class, out of which one is public. If main method is inside a non-public class. I can't run that java file. Why is that? and there is no compilation error as well. If so, how can I use that main method?

推荐答案

你当然可以覆盖main方法,它不违反任何编译规则因此你不会有任何编译器错误。

You can certainly override main method and it does not violate any compiler rules and hence you will not have any compiler errors.

你检查一下,尽管你有一个以上的类,一个声明为public的文件名是您尝试执行的文件。

You check that inspite of the fact that you have more than one class a file that is declared as public is the name of the file you are trying to execute.

这是一个约定,该文件应该在该代码中公开的同一个类之后命名

因此,当你尝试执行该类时,它没有一个主方法从中开始执行。所以如果你想在非公共中执行main方法class唯一的方法就是从公共类的main方法调用main。

Hence when you try to execute that class it does not have a main method from which it starts execution.So if you want to execute the main method in the non public class the only way to this is call that main from a main method of the public class.

这篇关于如果主要方法在“非公共类”内部怎么办?的java文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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