Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can jvm enter in default class:

class try1
{
public static void main(String args[])
{
    ...
}
}

In it how does JVM access this method?

In packages, if a class is 'default' its public methods cant be accessed from outside the package, so how does jvm enter this class?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
223 views
Welcome To Ask or Share your Answers For Others

1 Answer

It is not JVM itself who invokes main method. This is rather a job of Java launcher, i.e. java.exe.
Java launcher is a small program written in C that uses regular JNI functions:

  1. JNI_CreateJavaVM to create a new instance of JVM and to obtain an instance of JNIEnv;
  2. JNIEnv::FindClass to locate the main class specified in the command line;
  3. JNIEnv::GetStaticMethodID to find public static void main(String[]) method in class #2.
  4. JNIEnv::CallStaticVoidMethod to invoke the method found in #3.

In fact, JNI allows you to work with all classes, methods and fields, even with private modifier.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...