• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

main() method access modifiers

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've seen what looks to be a little bit of confusion on what access modifiers can be used for the entry-point main() method in an application.
Obviously, if placed individually in a file, any of these will compile fine:
public static void main(String[] args) {...}
static void main(String[] args) {...}
protected static void main(String[] args) {...}
private static void main(String[] args) {...}
because they can just be considered normal methods. The question is whether they are legal entry point into an application.
In compiling and running examples of each, I've seen that any of those 4 main() methods will work as an entry point. However, I've also seen comments that this behavior is a bug, and that officially they are not supposed to work (except for "public", of course), and thus, should be considered wrong on the Exam.
Does anyone know for sure how it should be answered on the test?
Thanks in advance,
Dave
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can use any of the access modifiers on the main method and it works. Sun does not consider this to be a problem.
When taking a certification exam ALWAYS say the main should be called with
public static void main(String args[])
The only changable part of that syntax is the variable name that you use for args (it can be anything).
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and wht about only
static void main(String args[])
protected static void main(String args[])
if asked in the exam if these r correct ways to define
the main() method then can v say 'yes' , these r valid ways or
no???
someone pls be more specific???
and wht about final public static void main(String args[])
???
Kamal
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you be more specific than "always"?
Yes, as I said, ALL of the access modifiers work with main, including "friendly", protected, private and public.
Sun has been hit up with questions on this topic, and had to defend themselves publically over and over. I truely do not think that they would choose this topic to argue over in a certification exam. But when YOU code a main on the exam, do it the proper way.
However, there is NOTHING that prevents main from being final.
 
dave taubler
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy,
Thanks for your responses. For some reason, this seemingly innocent and easy topic has been a source of confusion for everyone I've talked with.
Dave
 
crispy bacon. crispy tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic