wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JQplus Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JQplus Question" Watch "JQplus Question" New topic

JQplus Question

Anon Ning

Joined: Mar 09, 2001
Posts: 19
hi all:
See the following question:
Which modifiers would be valid in the declaration of a main() method so that the class can be run from command line?
a) native
b) protected
c) public
d) final
e) abstract
The answer is a),c),and d).
I don,t know why choose a)? Why b) is not correct?
Please help me!
In addition,how about JCertify mock exam? Is it worth to buy?
Sathi Chowdhury
Ranch Hand

Joined: Mar 16, 2001
Posts: 52
ans a is not correct,because native methods can not have a body
Sathi Chowdhury
Ranch Hand

Joined: Mar 16, 2001
Posts: 52
I think b is a correct option also
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi guys,
The only acceptable modifiers of <code>main()</code> are public and final.
You can declare main() with other modifiers ie protected, private, abstract (with no body) or native (with no body) but the compiler will not recognize such a method as the starting point of an application.
Hope that helps.

Jane Griscti
Sun Certified Programmer for the Java� 2 Platform

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3253
You can declare the main() as native as well. The JVM doesn't care about whether it has a body or not. All it cares is whether a public static method named main, returning void and that takes a string array as a parameter, exists for the class or not. If it exists, it will pick it up and invoke it.
If you make the method native, the jvm will try to look for the "body" in shared lib or dll. If it finds one, well and good and if it doesn't find one, it will throw an UnsatisfiedLink error.
Get Certified, Guaranteed!
(Now Revised for the new Pattern)

Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91
following code comiled but did not run:
class MethodMain {
final static void main() {
i compiled the code. it compiled but did not run.
here is a table fo what i did:
Compiled Runtime
protected yes no
final yes no
(with body) no no
(with body) no no
(without body) yes no
jane is right: only the canonical signature (public static void main() )is a legal entry into an application.
Mafalda Alabort

Joined: Mar 02, 2001
Posts: 24
Hi all,
I compiled and run this code with no errors.
public class Q1{
private static void main(String [] args){
What is going on? Is this somehow platform dependent?
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Mafalda,
If you are using JDK 1.2 or 1.3 ... yes, main() will compile and run when it's declared with a 'private' or 'protected' modifier. There's a big controversy over this. Sun says it's not a bug, others argue that it is. If you do a search in this forum you'll find a number of threads on the topic.
For the purpose of the exam; only 'public' and/or 'final' modifiers are acceptable (along with 'static' and 'void' which must be part of the declaration)
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
I agree. Here's the link: http://aspose.com/file-tools
subject: JQplus Question
Similar Threads
casting reference
what are mandatory elements in TLD?
Package and Overriding
A cast problem!!
Why is the include JSP Standard Action like SSI?