Java Understanding the basic Java method in logic

The main method is the most important method in your Java application to launch your application as an entry point. What happens before using this method is unclear. Please someone can help me understand / clarify what happens before the method is used, correcting my perception based on the method signature as follows:

  • The JVM creates at least one object that will access your main method. This (supposed) object is trying to access your Java application according to the API, which obviously connects you to the well-known method signature public static void main (String[] args){}

    • public You can not restrict the (alleged) single object in the JVM from accessing the object, where the main method is fully considered only in the logic and not in the API / signature?

    • static There are simply no objects and starts to create any other instances of objects, yet (except for the intended JVM), to create or create objects from it. A static modifier implies the only possibility of access to this method, since it is not tied to an instance and can be accessed, therefore 2 without an instance. Again, this is logical, since without any objects and launch (other than the intended JVM one), there still cannot be any objects to create any other objects with?

    • args Standard for different languages ​​and applications / executables to provide application customization? |

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

+3
java main
source share
4 answers

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

The Java virtual machine is started by creating an initial class, which is specified in an implementation-specific way, using the boot class loader (section 5.3.3). Then, the Java virtual machine binds the source class, initializes it, and calls the public class void main (String []) method. Calling this method leads to further execution. Following the Java Virtual Machine instructions that make up the main method can result in the binding (and therefore creation) of additional classes and interfaces, as well as the call of additional methods.

In a Java virtual machine implementation, the source class can be represented as a command line argument. Alternatively, the implementation may provide an initial class that installs the class loader, which in turn loads the application. Other variations of the source class are possible if they meet the specifications specified in the previous paragraph.

There are other descriptions in the JLS 12.1 section.

The JVM directly calls the main method - you do not need to create a new object for this. Although the main method itself must be publicly available, the class that it declared is not. For example:

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

Then do with:

 java 'Test$EntryPoint' 

He prints "Hello," as expected.

No code outside the Test class has access to EntryPoint.main() , except through privileged reflection, or direct access, which the JVM explicitly runs on.

+7
source share

java first loads its kernel - java.lang, class loaders, system properties, runtime, etc., and then looks at what it should do. Before initializing the JVM, there is no "java" in this process. This is just a native process, and so I think it would be wrong to think in Java before this happens.

Now, the JVM launcher first looks at the pre mains, calls them in order (first calls the corresponding static blocks), and then looks at the main method, calls the classes static blocks (s), if any; finally, call the main method, passing any command line arguments to the primary and primary 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 "); } 

}

+2
source share

When you enter the command as java someClassName, then the current thing happens. 1. Download the someClassName class and execute a static block (if any). When you load the class, an Object class will be created that will represent your class. 2.It call the main method using the class name (it will not create any object of your class) that the main method is static.

0
source share

Say the file Demo.java contains the source code as

 public class Demo{ } 

when this code is compiled because it successfully compiles as

 $javac Demo.java 

but when it runs like

 $java Demo 

then it shows an exceptional error

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

therefore, the compiler is not responsible for checking whether the main() method exists or not. The JVM is responsible for this. The JVM checks the main() method with prottoype as

 public static void main(Strings[] args) 
  • Why is the JVM looking for the main() method? Is it possible to change the main() method to any other main_hero() method?

The JVM is instructed to find the main() method from within the JVM. Yes, it is possible to change the main() method to the main_hero() method, but inside the JVM you need to specify the main_hero() method.

 JVM{ public static void main(String[] args) } 

to

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

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

  1. Why static?

main() method is not associated with an object, therefore, without an existing object, the JVM must also call this method. main method has nothing to do with the object.

  1. Why void?

The JVM will call the main() method, but what can the JVM do with the return value if the main() method returns. So it means void .

0
source share

All Articles