aspose file tools*
The moose likes Beginning Java and the fly likes simple main function doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple main function doubt" Watch "simple main function doubt" New topic
Author

simple main function doubt

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

we usually write





can we write the main function as


http://www.lifesbizzare.blogspot.com || OCJP:81%
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

why not ! but Obviously it would not called by the main thread.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

@sudhhkar you mean it wont function as an entery implementation point of the code
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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 ??


JDBCSupport - An easy to use, light-weight JDBC framework -
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
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

Joined: Dec 22, 2009
Posts: 2
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.


Learn Java: http://polylingualprogrammer.com
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11476
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

okk is Main class in lang package??
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple main function doubt