• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Accessibility of main()

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thandapani,

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

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
See where your hand is? Not there. It's next to this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic