• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple main function doubt

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we usually write





can we write the main function as
 
Sudhakar Sharma
Ranch Hand
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not ! but Obviously it would not called by the main thread.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@sudhhkar you mean it wont function as an entery implementation point of the code
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey ...

there is nothing wrong with having a void main function... it's perfectly legal..

but think about it, upon project launch, no object instances exist. if you don't mark main static, what instance would main be invoked on ??
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write main function the way you want but remember - only public static void main(String args[]) is the only valid function that is searched by the JVM when you run a java application.
 
Geoff Prewett
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need the "public" so that code outside the class (namely the JVM's class loader) can call the function. You need the "static" so that the function can be called without an object (since it is not clear how one would instantiate the class, as there might be multiple constructors). It needs to be called "main" because the JVM has to have something to search for, and "main" is the canonical name. And it has to have String[] as its argument so that the JVM can pass in the command line arguments.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say you CAN write A function called 'main' with that signature...but it will NOT be called by the JVM at startup. it will be like any other "void <name>(String [])" method.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okk you mean


is like telling JVM to startup


and on the other habd if its like

or

it wud be like a user -defined function isnt it
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is like telling JVM to startup

Almost. The JVM looks for a method whose signature is public static void main(String args[]) in the main class, which is the entry point of your program. Any other signature is valid, but will not be recognized as the entry point.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okk is Main class in lang package??
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:okk is Main class in lang package??

The main class I'm talking here is the one you pass to java.exe on the command line. (e.g java HelloWorld)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic