本文介绍了Java理解Java逻辑的主要方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就启动应用程序为切入点而言,main方法是Java应用程序中最重要的方法.使用此方法之前会发生什么尚不清楚.请有人通过以下方法签名来纠正我的看法,以帮助我理解/阐明使用该方法之前发生的情况:

The main method is the most significant method in your Java application with regards to launching your application as the entry point. What happens prior to this method being used is unclear. Please can someone help me understand/clarify what happens before the method is used by correcting my perception thereof based on the method signature as follows:

  • JVM至少创建一个对象,该对象将访问您的主方法.这个(假定的)对象尝试根据该API来访问您的Java应用程序,该API显然会将您绑定到已知的方法签名public static void main (String[] args){}

  • public您是否不能限制JVM上的(假定的)单独对象访问包含主要方法的对象,而该方法主要仅查看逻辑而不是API/签名?

  • public You can't restrict the (assumed) solitary object on the JVM from accessing your Object housing the main method completely looking at logic alone and not the API/signature?

static根本没有任何对象可以运行或创建任何其他对象实例(假定的JVM实例除外)来实例化或创建其他对象.静态修饰符表示访问此方法的唯一可能性,因为它没有绑定到实例,因此可以在没有实例的情况下访问.再说一次,这是逻辑,因为没有任何对象启动和运行(除了假定的JVM),还没有任何对象可以实例化任何其他对象吗?

static There are simply no objects up and running to create any other instances of objects up yet (other than the assumed JVM one) to instantiate or create objects out of yet. The static modifier implies the only possibility of accessing this method as it is not bound to an instance and can be accessed therefore 2ithout an instance. Yet again this is logic as without any objects up and running (apart from the assumed JVM one), there can't be any objects up yet to instantiate any other objects with?

args跨语言和应用程序/可执行文件的标准以提供自定义应用程序的功能吗?|

args A standard across languages and applications/executables to provide ability to customize the application?|

这是接近并理解主要方法的正确且合乎逻辑的方法吗?

Is this a correct and logical way to approach and understand the main method?

推荐答案

尚不清楚您真正要问的是什么,但是JVM规范5.2节至少涵盖了其中一些内容:

It's not entirely clear what you're really asking, but the JVM specification section 5.2 covers at least some of this:

在Java虚拟机的实现中,可以将初始类作为命令行参数提供.或者,该实现可以提供一个初始类,该初始类设置了一个类加载器,该类加载器又加载了一个应用程序.初始类的其他选择也是可能的,只要它们与上一段中给出的规范一致即可.

In an implementation of the Java Virtual Machine, the initial class could be provided as a command line argument. Alternatively, the implementation could provide an initial class that sets up a class loader which in turn loads an application. Other choices of the initial class are possible so long as they are consistent with the specification given in the previous paragraph.

JLS第12.1节还有其他一些内容也是说明.

The JLS section 12.1 has some other descriptions too.

JVM会直接调用main方法-无需创建新对象即可.尽管main方法本身必须是公共的,但声明它的类却不是.例如:

The JVM invokes the main method directly - it doesn't need to create a new object to do so. Although the main method itself has to be public, the class it's declared in doesn't. For example:

public class Test {
    private static class EntryPoint {        
        public static void main(String[] args) {
            System.out.println("Hi");
        }
    }
}

然后执行:

java 'Test$EntryPoint'

它按预期打印"Hi".

It prints "Hi" as expected.

Test类之外的任何代码都不能通过特权反射来访问EntryPoint.main(),或者可以通过JVM明确地直接访问.

No code outside the Test class has access to EntryPoint.main() other than through privileged reflection - or direct access that the JVM is clearly capable of.

这篇关于Java理解Java逻辑的主要方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 08:19