• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JQplus Question

 
Anon Ning
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks!
 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ans a is not correct,because native methods can not have a body
 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think b is a correct option also
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3752
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
mansoor iqbal
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
following code comiled but did not run:
[CCODE]
class MethodMain {
final static void main() {
System.out.println("hi");
}
}
[/CODE]
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
abstract
(with body) no no
native
(with body) no no
native
(without body) yes no
jane is right: only the canonical signature (public static void main() )is a legal entry into an application.
 
Mafalda Alabort
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I compiled and run this code with no errors.
public class Q1{
private static void main(String [] args){
System.out.println(args[0]);
}
}
What is going on? Is this somehow platform dependent?
Thanks
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic