简体   繁体   中英

Java Understanding Java main method on logic

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?

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.

java first boots up its core - java.lang, classloaders, system properties, runtime etc and then looks at what it has to do. Before the JVM is initialized there is no "java" in that process. Its just a native process and so I think it would be wrong to think in Java terms before this happens.

Now the JVM launcher would first look at pre mains, call them in order (first calling respective static blocks) then look at the main method, call that classes static block(s) if there are any; finally call the main method, passing any command line arguments to the premain and main methods.

Simple Tests:

public class Test {

    static{
            System.out.println("Hi static 1");        
    }

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

    static{
        System.out.println("Hi static 2 better to have 1 static block per class but you can have N ");        
    }

}

When you put the command like java someClassName then the flowing thing happen. 1.It load the class someClassName and execute the static block(if any) During class loading an Object class Class will be created which will represent your class. 2.It invoke the main method using the the class name(It won't create any object of your class) that why main method is static.

Say, file Demo.java contains source code as

public class Demo{ 

}

when this code is compiled as it's compile successfully as

$javac Demo.java

but when it's executed as

$java Demo

then it shows an exceptional error

Main method not found in class Demo, please define the main method as: public static void main(String[] args)

so compiler is not responsible to check whether main() method is present or not. JVM is responsible for it. JVM check for main() method with prottoype as

public static void main(Strings[] args)
  1. Why JVM search main() method? Is it possible to change main() method into any other method main_hero() ?

JVM is instructed to find main() method from inside JVM. Yes, it's possible to change main() method into main_hero() method but inside JVM you must have to instruct to search main_hero() method.

JVM{ 

    public static void main(String[] args)  
}

to

JVM{ 

    public static void main_hero(String[] args)     
}
  1. Why public?

JVM is installed either in C or D drive so to call from anywhere public is used.

  1. Why static?

main() method is no related to object so without existing object also JVM has to call this method. main method no way related to object.

  1. Why void?

JVM is going to call main() method but what can JVM do with return value if main() method return. So it's meant to be void .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM