Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessibility of main()

 
Greg Torrance
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I am new to the forum - (great group BTW!) - and was wondering if someone can clear something up for me. I keep reading that the main() method must be defined as public and static, but when I run a test with a private or protected main() method, it works. Why is this?
For example, if I create class Test1 as follows:
class Test1
{
private static void main( String[] args )
{
System.out.println( "private main" );
}
}
And then I run it as
java Test1
I get the "private main" message.
Any thoughts?
Thanks,
Greg
 
Thandapani Saravanan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually this has already been discussed a couple of times. As per JLS the method must have the following signature:
public static void main(String args[])
If the compiler allows other versions, then we could say it is a bug in compiler. At the same time, we don't get anything more by changing the specification, do we?
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thandapani,

Is that really a bug or there is something we are really missing...

 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep !! it was a bug, in older java versions that is prior to java 1.4 , we can make main method as default or private.


Though it has been rectified now.


-faisal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic