• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Public static void main()

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it always necessary to pass argument to public static void main() . i have run program throug eclipse and it says that it cant find main().

code:
class First()
{
public static void main()
{
System.out.println("Hello");
}
}

Whats the problem?
And is it necessary to pass argument(String[] args) to main()? If yes, then why?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i have run program throug eclipse and it says that it cant find main().
code:
class First()
{
public static void main()
{
System.out.println("Hello");
}
}
Whats the problem?
And is it necessary to pass argument(String[] args) to main()? If yes, then why?


No. Your main method's signature is wrong. You should add "(String[] args)" part as well, JVM will look for this signature when you execute the class.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The real answer is, of course, that this is just how they chose to do it. A non-static main() would have worked just as well; the JVM could create an instance of main()'s class to call it if it needed to. But since the JVM is designed to expect a static method, that's what you must use.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be the question is wrong. It is not necessary to pass arguments to public static void main() but it is mandatory to have a parameter of the String object i.e String[] or as of Java5 a var-arg String parameter i.e String...
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anshul Agrawal wrote:... A non-static main() would have worked just as well; the JVM could create an instance of main()'s class to call it if it needed to.


That would be an additional work for the JVM
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, definitely the main method in Java needs to have a parameter which should be either String[] or String...(Java 5 onwards) and this is because that is the signature for the main method that the JVM expects.
 
Oh. Hi guys! Look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic