Author Topic:   Mughal's Mock Exam-Main method declaration
Betty Reynolds
ranch hand
posted April 06, 2000 11:30 AM         
In regard to this question:

code:

Which modifiers and return types would be valid in the declaration
of a working main() method for a Java standalone application?


1. private
2. final
3. static
4. int
5. abstract


The correct answer is 2 and 3 (by the "answer 1 question method" of finding out the right answer), but you can successfully compile and run with a private modifier. He also makes the same point in his book on p. 39 and on p. 625, where he basically says that a valid declaration of main must always be public and static, but static seems to be the only real modifier requirement.


[This message has been edited by Betty Reynolds (edited April 06, 2000).]

sree
ranch hand
posted April 06, 2000 12:36 PM             
Can we have "final" keyword in the main() declaration.

Betty Reynolds
ranch hand
posted April 06, 2000 12:47 PM         
Yes, sree. My point is that private, final and static all seem to be valid, not just final and static.

maha anna
bartender
posted April 06, 2000 01:06 PM             
Betty,Sree,
According to JLS the req. for a perfect main methods is as foll.
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

So I think we shd follow this.
regds
maha anna

John Wetherbie
ranch hand
posted April 06, 2000 01:08 PM             
Having final as a modifier on a main method does compile and run.

Using private (or default, or protected) access will compile but I receive the message: In class X: main must be public and static when I try to run the program.
(I'm running the latest JDK 1.2.2 prod release for Windows)

Doesn't appear to care about final but as far as I can tell using private won't run. Anyone know why final is OK? Is it because inheriting the main function doesn't make sense semantically?

Jim Yingst
sheriff
posted April 06, 2000 01:12 PM             
[This was composed without knowledge of the last five or so replies, so sorry for the redundancy - I'm too lazy to re-edit it now for conciseness - Jim]

The says: "The method main must be declared public, static, and void." Abstract is not possible because abstract conflicts with static (and obviously private would conflict with public). Nothing is said about final, so there is no reason it can't be used. And the existing jdks do support it.

As for public being required - it is true that in actuality curren t jdks do seem to allow any access modifier for main(). However this violates the JLS. Well maybe "violates" is a little strong - the JLS doesn't say what the compiler or JVM must do if the method is not public. I suppose from the jdk's perspective, it's undefined, and they can choose to allow it. However, we the users have been warned against it - and if a new jdk comes along which throws an error for a private main, it's entirely compliant with the JLS. If that breaks existing code, that's our fault for writing code that violates the JLS. So on this point I'd say M & R are right, though it might be nice if they pointed out that current jdks were... misleading in this respect.

I'm moving this to Mock Exam Errata.

[This message has been edited by Jim Yingst (edited April 06, 2000).]

|