• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt it or not?

 
Roger Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all:
Q:In order for a source code file, containing the public class Test, to successsfully compile, which of the following must be true?
Here is a option :
(a)It must declared a public class name Test.
As I known, a file with a class with default modifier can also compile and run successfully.
Choose (a) or not? In my opinion, I will choose it even if the fact above exists.
Thanks.
Roger
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JLS, §12.1.4 Invoke Test.main:

The method main must be declared public, static, and void. It must accept a single argument that is an array of strings.

I realize that some compilers will execute even if the main method doesn't include the keyword public but, according to the JLS, it must be public. Therefore, as far as the exam is concerned (because the exam is based on the JLS, not every Java compiler on the market), the answer is that the main method MUST be public.
I hope that helps,
Corey
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it, this was never an enforced constraint until jdk 1.4. However, it is certainly the way things are supposed to work.
M
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic