Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) 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
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!