• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

try again

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Q11
{
static String str1 = "main method with String[] args";
static String str2 = "main method with int[] args";

public static void main(String[] args)
{
System.out.println(str1);
}

public static void main(int[] args)
{
System.out.println(str2);
}
}
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
huh ?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently, this thread was meant as a follow-up to a prior post:

https://coderanch.com/t/398989/java/java/program

Now that the errors are corrected, this code compiles and runs fine. So how do we explain its behavior?

Section 12.1.4 of the Java Language Specification says that as an entry point, "the method main must be declared public, static, and void. It must accept a single argument that is an array of strings."

Ref: http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#47024
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now that the errors are corrected, this code compiles and runs fine


for the first time in my life i see two main in the same class

C:\>java -cp . Q11
main method with String[] args

C:\>


[ March 16, 2005: Message edited by: miguel lisboa ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second main is a perfectly valid method -- it just won't be called by the JVM as an entry point. Using the correct argument, we can call main(int[]) just like any other method...
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes:

C:\>javac Q11.java

C:\>java -cp . Q11
main method with String[] args
main method with int[] args

 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic