• Post Reply Bookmark Topic Watch Topic
  • New Topic

in which class main() method resides in java API?  RSS feed

 
jayanandan thiagarajan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i am confused with the main() method.coz,from where we inherit this method?.I think it should be inside any one of the Java API class,from where we inherit.Please help me to know this in detail.

Thanks in advance.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main method is not inherited from anywhere. You have to supply a main method in order for your application to run. If you don't supply a main method you will get an error when you try to run your application.
 
jayanandan thiagarajan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you.
So it is written inside the interpretor that to see the main() in all classes while executing.
am i right?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What sort of application are you thinking of? Standalone app -- in this case you must be typing something like this on the command line:

>java MyClassWithAMain

or you are using a bat file (or equivalent) that is doing this for you.

If you are thinking of an applet, the "main" is part of the code that IE or Netscape has embedded in it (the plugin) and you never see it.

If you are thinking of a web app, again the "main" is part of the framework and you never see it, or write it.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jayanandan thiagarajan:
thank you.
So it is written inside the interpretor that to see the main() in all classes while executing.
am i right?


Yes. When you run
java MyClass
Java will look for a method with the signature
public static void main(String [] args)
in the MyClass.class file. If it doesn't find it, it will fail.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is NOT required to have a main() method in every class. you can write an application with dozens or hundreds of classes, but only one has to have a main() in it - the one you call to start your program.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the fact that the JVM looks for main() on the specified class when starting up is the only magic thing about main. It's really a perfectly normal static method with no magic powers of its own.
 
stephen shields
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you wish for an class to be a starting class then yes it must have a main(String[] args)
this does not apply to an applet
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!