my dog learned polymorphism*
The moose likes Beginning Java and the fly likes main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "main method" Watch "main method" New topic
Author

main method

shandilya popuru
Ranch Hand

Joined: Dec 21, 2004
Posts: 95
hi all

public class test
{
static void main(String a[])
{
int d1=10;
char c='a';
System.out.println(d1+c);
}
}
this code gives the same o/p as this

public class test
{
public static void main(String a[])
{
int d1=10;
char c='a';
System.out.println(d1+c);
}
}

so even if main method isn`t decld public it works fine
plz explain me what is the diff


sandy
Natesh Kurup
Greenhorn

Joined: Nov 23, 2004
Posts: 19
it works fine bcz its the earlier code which is getting executed. Rename the second class, it will compile but wont execute.

regards

natesh
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Originally posted by NateshK Kurup:
it works fine bcz its the earlier code which is getting executed. Rename the second class, it will compile but wont execute.


No. There is a possibility that it might execute even after renaming!

Originally posted by shandilya popuru:
so even if main method isn`t decld public it works fine
plz explain me what is the diff


The problem here is with the JVM version you are using. With the earlier JVMs, it is mandatory that you have to specify the main() method as public.
But later, for some reasons, some versions allowed (1.2.2 & 1.3 I think) you to have non-public main method.
If you try the same code with version 1.4, you will get an Runtime error saying "Main method is not public".
Don't know what is the case with 1.5. Anybody who has 1.5 installed can throw some light.


Mani
Quaerendo Invenietis
Nitin Jawarkar
Ranch Hand

Joined: Dec 18, 2004
Posts: 79
hi,

public means u r main method able to access globaly in the class where u r main method is.
Thanx. :roll:


Cheers<br />Nitin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main method