• Post Reply Bookmark Topic Watch Topic
  • New Topic

main method vs other normal method  RSS feed

 
Ranch Hand
Posts: 500
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code will not compile we all know this. 2 errors are there in this code
1. spelling of main() method. it should be main() not Main()
2. getAngle() doesnot have body

but the question is "why this code is not compiling". In my opinion option 1 should be preferred first. But the given answer is option 2.

why is it so??
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method Main() is just a regular method, not a compiler error.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main might be poor style but it is not a compiler error. The empty method is a compiler error however.
 
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing inherently magic in naming a method main or Main.

However, the "java" program will look for a method whose signature is "void main(String[] args)" in the class you indicate. And if it finds such a method, then java will invoke it. And if it doesn't execution of the application fails.

But that's runtime. At compile time, the presence or absence of a properly-configured "main" method doesn' matter (to the compiler!)
 
Puspender Tanwar
Ranch Hand
Posts: 500
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. got the point. main() method is checked at the runtime, not the compile time that's why answer is 2 option. But please tell me how I can identify that a particular declaration is checked at runtime or the compile time. Really confused in runtime and the compile time. Like why the "could not find the main method in class" is at the runtime, why not at the compile time ???
 
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the compiler has been designed to generate bytecode(.class file) from .java file irrespective of there is main method or not. As long as the syntax is valid and dependencies are resolved, compiler will not complain.
It has not been designed to check main method availability.

But when you are running a Java program, it first searches for a main method because that is how it has been developed.
main method is the entry point of any Java program.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!