Hi Suehyun Lim,
First of all, a warm welcome to CodeRanch!
Suehyun Lim wrote:In the review question #4 in chapter 1 of OCAJP 8, I don't understand why the code compiles. It's a public class, and it doesn't have a main() method. Doesn't that mean that it won't compile?
The
main method is not required to successfully compile the class. So if a class has valid syntax, but no
main method, the class will still compile successfully. For example, these classes will all compile successfully (although none of them defines a
main method)
If you want to execute the class, you need a
main method with the required method declaration
So this class will compile, run and print
"Java rocks!"But although this class compiles successfully, it won't run. Can you see why?
Hope it helps!
Kind regards,
Roel