• 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

All about main()

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
The main() method is essential to all java programs, that need to be executed (Execution of any java program begins at main()). All predefined methods are declared/defined in some class(eg: concat() in the String class or drawRect() in the Graphics class).
Now WHERE is the main() method declared/defined? Its not present in the Object class, where I presumed it to be .
Or is it that the main() method is a user-defined method. If so, why can't we say, or .

Could someone, please explain this.
Thanx in advance
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main() method is a user defined method, you have to write what you want your app to do in the main method.
I'm not sure, but I think the reason that you can't change the name of the main method is because when you run the app by typing in java appname, java.exe searches for the main method, and if it doesn't find it, it won't run.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rebecca,
The public void static main(String[] argv) method is a method you would add to your own class if you wanted to start the program execution from this class.
For example,

After you compile the above code with the JAVA compiler, you can execute the program by typing: java MyClass.
From the JVM specification:

The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (�5.3.1). The Java virtual machine then links the initial class, initializes it, and invokes its public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java virtual machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

-Peter
[This message has been edited by Peter Tran (edited January 08, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic