• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

try again  RSS feed

 
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);
}
}
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
huh ?
 
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
 
Ranch Hand
Posts: 1282
  • 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: 1282
  • 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

 
Maybe he went home and went to bed. And took this tiny ad with him:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!