This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main method query

 
Shankar Salunkhe
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Shankar Salunkhe
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Vinod Tiwari
Ranch Hand
Posts: 466
1
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shankar Salunkhe
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinod,

Very helpful blog that you have mentioned.........Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic