This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why is main not a key word? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why is main not a key word?" Watch "Why is main not a key word?" New topic
Author

Why is main not a key word?

Sandra Dakurah
Greenhorn

Joined: Dec 01, 2004
Posts: 2
Hi everybody,

I just started studying for the java 2 programmers exam. Can anybody tell me why main is not a key word? Can I use main for any variable or function name? (I hope not).

Sandra
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Well, give it a try. I'm reminded by the compiler every once in a while that I can't use "return" as a variable name.

In fact, main() is a method like any other. The *only* special thing about it is that when you give a class name at JVM startup the JVM calls the main method on that class with an array of strings.

The Java In General forums are good place for general questions ... this one is focused on servlets.

Cheers!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Moving to the SCJP forum.

This forum is for Servlets questions.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

A main method has "special meaning" (it can be used as an entry point) only if it is public and static, returns void, and takes a String array as its argument. That is...

public static void main(String[] args){}

As an entry point, main can also be modified with synchronized, strictfp, or final. Note that the String array can be labeled with any legal identifier, and this variable is local to the method, so you can define String[] args (or whatever you want to call it) outside of main as well. But "main" itself is not a keyword, so it can be used as an identifier of anything you like.

(Note that prior to 1.4, the entry-point main was not required to be public.)
[ December 01, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Below is some illustrative (although generally inadvisable) code...


Yes, this compiles and runs.
[ December 01, 2004: Message edited by: marc weber ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why is main not a key word?
 
Similar Threads
public static void main method
Assertions
discusss question about the key word and reserved word
constructor
implements not there in jsp page directives