File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main method

 
shandilya popuru
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Natesh Kurup
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitin Jawarkar
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

public means u r main method able to access globaly in the class where u r main method is.
Thanx. :roll:
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic