• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

simple main function doubt

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





can we write the main function as
 
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: 1087
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
 
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 ??
 
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.
 
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.
 
lowercase baba
Posts: 12751
51
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: 1087
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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: 1087
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 VI Editor Ubuntu
  • 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)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!