5

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?

thejartender
  • 9,339
  • 6
  • 34
  • 51
  • 3
    "The JVM creates at least one Object that will access your main method." - What leads you to that conclusion? (I believe it will always create a string array, but that's not an object "that will access your main method".) – Jon Skeet Apr 10 '13 at 10:03
  • 1
    main is static need no object to access. – Ankit Apr 10 '13 at 10:03
  • FYI the main does not have to be the most significant. read up on pre_main can have some one for a few pre_mains that do all/ most of the work. I have used pre main to inject common System properties from a data base but they could do other work too, like instantiating user classes – tgkprog Apr 10 '13 at 10:33

4 Answers4

9

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.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @ Jon. The question may seem odd, but is founded on hindsight with understanding of the keywords -something myself and most likely the majority of Java developers did not know of when they first learned about the main method. – thejartender Apr 10 '13 at 10:22
  • i think the keywords dont have much value when its the JVM. Like you pointed out no other classes can access the private class Test $EntryPoint but the JVM does – tgkprog Apr 10 '13 at 10:31
  • @tgkprog: The question is more unclear than odd. What are you actually asking, and does my answer provide answers to that? – Jon Skeet Apr 10 '13 at 10:33
  • my observation was that private has no meaning to the JVM when it needs to access something a class; as long as its privileged security (and the initial app security is the main everything allowed) – tgkprog Apr 10 '13 at 10:54
  • 1
    @tgkprog: Absolutely. The JVM can do what it wants. User code is restricted by security policy. – Jon Skeet Apr 10 '13 at 10:59
  • Whoops - my earlier question was meant to be directed at the OP. Will repeat it :) – Jon Skeet Apr 10 '13 at 11:00
  • @thejartender: The question is more unclear than odd. What are you actually asking, and does my answer provide answers to that? – Jon Skeet Apr 10 '13 at 11:01
2

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

}

tgkprog
  • 4,493
  • 4
  • 41
  • 70
0

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.

Krushna
  • 5,059
  • 5
  • 32
  • 49
0

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.

René Vogt
  • 43,056
  • 14
  • 77
  • 99