0

how java find main function.

As I get, the main class inn a java code should be inside a class. And as i know preveiwsly in other languages for example c++, we shoud define and call classmethods to works. But here in hello eorld sample i see that the main method is inside of a class. so ho the compiler can find it without defining? If we have 2 classes what will hapend then? i guss it should be forbidden to have 2 classes with main methot. but my main questios is still how can it find "main?

26th Sep 2016, 3:47 AM
Hassan Bagheri Valoujerdi
Hassan Bagheri Valoujerdi - avatar
1 Odpowiedź
0
Short Answer: Parsers! Long Answer: The main method can only exist within one class. If there are two different classes that have the main method, then java will throw an error and not allow you to compile the code. Java knows where the main class is through what are called "parsers". Before the Java program is compiled, the code is ran through a parser. A parser will read the source code to make sure your syntax is correct (i.e. the main method exists). If your source code goes through the parser without any errors, it is then compiled following the instructions in the main method. Take a look at these pages to further understand parsing and compiling: https://en.wikipedia.org/wiki/Lexical_analysis https://en.wikipedia.org/wiki/Parsing
26th Sep 2016, 4:08 AM
julie