Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Understanding Java main method on logic

Tags:

java

main

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:

  • The JVM creates at least one Object that will access your main method. This (assumed) object attempts to access your Java application according to the API which obviously binds you to the known method signature public static void main (String[] args){}

    • 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 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 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?

like image 819
thejartender Avatar asked Apr 10 '13 10:04

thejartender


People also ask

How is main () method declared in Java?

The declaration of the Java main method is: public static void main(String[] args) { //Your code goes here. }

Can I write logic outside of main method in Java?

You can write code outside the main. but remember that the code can only be accessed if there is a way to it from the main Method, example functions.

How JVM knows about main method in Java?

The Java compiler or JVM looks for the main method when it starts executing a Java program. The signature of the main method needs to be in a specific way for the JVM to recognize that method as its entry point. If we change the signature of the method, the program compiles but does not execute.


1 Answers

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

The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java Virtual Machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

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.

The JLS section 12.1 has some other descriptions too.

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");
        }
    }
}

Then execute with:

java 'Test$EntryPoint'

It prints "Hi" as expected.

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.

like image 105
Jon Skeet Avatar answered Sep 28 '22 06:09

Jon Skeet