aspose file tools*
The moose likes Beginning Java and the fly likes try again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "try again" Watch "try again" New topic
Author

try again

kumara swamy adurthi
Greenhorn

Joined: Mar 16, 2005
Posts: 4
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

Joined: Oct 23, 2002
Posts: 904
huh ?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
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 ]

java amateur
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Feb 08, 2004
Posts: 1281
yes:
C:\>javac Q11.java

C:\>java -cp . Q11
main method with String[] args
main method with int[] args
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try again