• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mughal's Mock Exam-Main method declaration

 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In regard to this question:

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. They also make the same point in their book on p. 39 and on p. 625, where it basically says that "a valid declaration of main must always be public and static", but it seems to me that static is the only real modifier requirement.


[This message has been edited by Betty Reynolds (edited April 06, 2000).]
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we have "final" keyword in the main() declaration.
 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, sree. My point is that private, final and static all seem to be valid, not just final and static.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[This was composed without seeing the last three replies, so sorry for the redundancy - I'm too lazy to re-edit it now for conciseness - Jim]
The JLS 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 current 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).]
 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, that's a really good point. It does seem to be implementation dependent. I can compile and run with private final static void main etc. just fine (I am running under the Windows 98 platform), so we need to heed the official pronouncements.
However, M&R states on p. 625

Also, declaring the method final does not affect the method's potential to be used as a main() method.

In your opinion, then, if this question came up on the exam, would you consider final wrong? Also, maha anna, would you consider final incorrect?


[This message has been edited by Betty Reynolds (edited April 06, 2000).]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John- I dunno, I can compile & run a private main method under jdk 1.2.2 or 1.3 beta for Windows (98), with no errors. Not sure why you would get a different result - I'd think any Windows environment should behave the same in this repect. But regardless, as per my message above, the compiler or JVM is perfectly justified in rejecting the private main. I don't know why in many case it does not.
Having final as a modifier doesn't strike me as particularly useful, but if someone wants to prevent any subclasses from defining their own main(String[]) method, they can. Not particularly useful, but no reason to forbid it either.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Betty- no, I would consider final right. It's allowed by the JLS and the djk.
Oops - I see that in all this, I misread what Mughal & Rasmussen's original answer was. Turns out I agree with them entirely. So this isn't a real "errata" after all, though I'll leave the thread here since it's something people might well think is an error, and this way they can find the discussion. Sorry for the mixed signals in my first post in this thread.
So, for clarity: M&R's answers 2 and 3 (only) are correct.

 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim - No you are right. I biffed. I have 4 computers at work I can use. One has Java 2 the others have 1.1.7 or 8. I ran my tests using 1.1.7. So for 1.2.2 for Windows using private (or the other access modifiers) runs.
I would consider this a bug (or a least a feature...)
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Betty,
For the Exam, I don't know. May be select/unselect/select/unselect... . First let me clear this doubt first.
Jim,
Can u answer for this? The 'final' for the 'main' method in an appln as such
doesn't affect the method's potential to be used as a main(..) method.
My doubt is in practice, can't we write an application which has 2 or more entry points. (i.e) There is a 'Base class' which has a 'main' as can be used as an independent appln. There is also a subclass of this 'Base class' which wants to have its own version of main(..) functionality but it wants to be a subclass of 'Base class' and re-use some of the code written in Base. This subclass also can be used as an independent application. So depending upon which class the user chooses to run, the effect is different (May be the GUI looks different depending upon the entry point.) So what do we do fot this type of requirements. If we define the 'main' in Base class as 'final' the subclass can't override this. So what Mughal says is ok for a single entry-point appln. This is what comes to my mind at present.What do you think Jim? Do I make sense?
regds
maha anna
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the real exam, I've never heard of a question about "private main" being on it. Not that I know all the questions they ask of course, but I think if "private main" were an issue on the real exam someone would have compliained about it. If I did see a question on it, I'd answer that it's not legal - given a choice between the jdk and the JLS, I favor the JLS.
Maha- right, final main would cause problems in the situation you outline. But it isn't fundamentally impossible to have a final main, it just puts additional restrictions on what you can do with it.
 
littlehead
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be the question about the overloading?
public class a{
public static void main(String args[]){}
private static void main(){}
}
?
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the private main working is definitely a bug in jdk1.2.2 on Windows NT. If you have it in Class A and Subclass B and subclass C donot have any main methods, they will still be able
to run (by accessing the private main of the superclass).
Is someone doing something about this?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic