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

question about arguments passed to main()

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i use
public static void main(int[] args)
the program will compile but give an error message
Exception in thread "main" java.lang.NoSuchMethodError: main
if an array of int arguments is not alowed, why does the compiler not get all uppity about it?
gotta use
public static void main(String[] args)
and make a new Integer object from the and call it's intValue() method to pass integer values from the command line? is this because the int is a primitive type, and only objects can be passed as arguments to main? why does the compiler allow int[] then?
just tried Integer[] as the argument to main and it gave the same error message. is String all that is allowed as an argument? seems that, as particular as the compiler is, is would flag anything else.
[This message has been edited by Eric Johnson (edited December 13, 2001).]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric
If you change the signature of the man method then all you are doing is creating an overloaded version of main. The compiler does not care if you overload a method or what you call your methods - but when you try to run the program the JVM will not be able to find the correct main method and throw an error because of that.
The compiler really can't check your main method because you don't have to have a main method in all of your classes, so the compiler gives you the benefit of the doubt and assumes you really meant to overload it and doesn't flag it.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you type an another signature for the main method (name + arg list) you are in fact creating another (different) main - so you may think you have an entry point into your system - but you don't - the compilier lets you compile a file without a main method in it but if you try to run that file you will get your error message: java.lang.NoSuchMethodError: main
Exception in thread "main"
------------------
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FYI - Not exactly what you are wanting to do, but this works as well
public static void main(int argn String[] args) {
}
Since args is an array, it will take in multiple values.
i.e. java myProgram now is the time
that will create a String array {"now", "is", "the", "time"}
but how do you get to each value? Well, the int argn keeps track of the array index. So args[argn] = the array.
Just a little info.


------------------
Happy Coding,
Gregg Bolinger
 
Eric Johnson
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx for the ifno!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic