• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is main not a key word?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the SCJP forum.

This forum is for Servlets questions.

Thanks

Mark
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is some illustrative (although generally inadvisable) code...


Yes, this compiles and runs.
[ December 01, 2004: Message edited by: marc weber ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic