This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Features new in Java 7 and the fly likes main method query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "main method query" Watch "main method query" New topic
Author

main method query

Shankar Salunkhe
Greenhorn

Joined: Jul 17, 2012
Posts: 10
Hi All,

I have gone through one small question session on core java wherein I have found the below question with answer.

Question : Can a main() method be declared final?
Answer : Yes. Any inheriting class will not be able to have it's own default main() method.

Does this question means we can write main method public final void main(String[] a) instead of public static void main(String[] a) ?

Can you please explain the actualy aim of this question.

Shankar
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Does this question means we can write main method public final void main(String[] a) instead of public static void main(String[] a) ?

No, main method has to be static, so in the case you want to make it final it's signature would be:


The quieter you are, the more you are able to hear.
Shankar Salunkhe
Greenhorn

Joined: Jul 17, 2012
Posts: 10
Thanks for your prompt reply.....

Can you please ellaborate the answer mentioned....[Any inheriting class will not be able to have it's own default main() method....]....

Any example would be helpful.....

Shankar

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

I can, but I would ask you first if you understand the meaning of the final modifier? Do you know what does it mean when you specify a method in your class to be final?
Shankar Salunkhe
Greenhorn

Joined: Jul 17, 2012
Posts: 10
About method.......If the method is declared as a final then that method we cann't override.......or overridden is not possible of the method.....
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Well, the same applies here. If you have a class with main method declared as final and extend it, the new (sub)class can't have method with the same signature - hence it cannot contain (what you say "default") main method
Shankar Salunkhe
Greenhorn

Joined: Jul 17, 2012
Posts: 10
Oooooops.....I am confused with this FINAL keyword......It plays different different role at each place........Thank you so much for your help......
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You're welcome.
Vinod Tiwari
Ranch Hand

Joined: Feb 06, 2008
Posts: 459
    
    1
Good example Kemal.

However main() method can be final and it mainly depends upon your requirement. Please read and intresting discussion on this on JR.


Vinod Tiwari | Twitter
Shankar Salunkhe
Greenhorn

Joined: Jul 17, 2012
Posts: 10
Hi Vinod,

Very helpful blog that you have mentioned.........Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main method query
 
Similar Threads
final in main method
final methods
Java Interview Questions
Ambiguous answer to anonymous classes
Help me please