Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try again

 
kumara swamy adurthi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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);
}
}
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
huh ?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently, this thread was meant as a follow-up to a prior post:

http://www.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
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes:
C:\>javac Q11.java

C:\>java -cp . Q11
main method with String[] args
main method with int[] args
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic