• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused about final static public void main(String []arguments)

 
Ira Jain
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
As per Khalid Mughal 's book and JLS ,you can use final modifier with main() .It has also been discussed in this forum http://www.javaranch.com/ubb/Forum24/HTML/001021.html
However,even after racking my brains alot ,I still cannot think of a situation where it can be used .Could somebody tell me of a situation where we can use it as such?
Thanks
Ira
------------------
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for making a method final is so that subclasses can not override that method. So the reason to make main() final would be for that reason. However, in a real application, you would generally only have one main method that kicks off your app, and I guess you could make this final so that no one can extend that class and override, but I have never used it before. I have put main methods in other classes for debugging purposes, but those mehtods I do want to override, so I wouldn't make those final. My only thought is that if you have one class whose main purpose is to kick off your app with all the initial values, you may want to make it final, so no one could override it. But like I said, I wouldn't really use this because if that class was just to kick off the app, that is not really a class I would be subclassing.
 
Rong Chen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
don't think static method can't be overriden, it can be hidden by implementation in subclass instead. either, i don't get the point why one would bother to write the entry of an application twice. but one thing for sure is that final doesn't appear in the recommended form of main() as an entry of an application anywhere. so, just forget it.
regards
rong
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have heard people before saying overridden and hidden. What is the difference between the two?
When you override a method, you have a subclass that has a method that is the same method name, with the same types and number of arguments. Thus you are overriding the method with the method of the subclass. There are other rules, like you can't make the method of the subclass more restrictive, can't make it non-static if it is static in the superclass, and you can't throw any checked exceptions that were not thrown by the superclass unless the checked exception is a subclass of the checked exception that was thrown in the superclass.
But what do you mean by hidden? I keep thinking they are the same, you have overloading and overriding and the later is the same as hiding.
Can anyone tell me the difference and if I am missing something?
Thanks,
Bill
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill!
here you can see what will happen if you attempt to "override" static method...
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, then from the message in the link it looks like:
Overriding is when you call Parent p = new Child(); and then when you call a method for p you will get the method that is in Child.
But Hiding is when you call Parent p = new Child(); and then when you call a method for p you will get the method that is in Parent. And the only way this happens is when you have a private method in Parent and then have the same method in Child but declared as public.
Still slightly confused, but I think I am getting it.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiding actually means you "shadow" the visibility of the method in the superclass. It has more to do with name-space overlaps than subclassing or inheritance.
You can observe runtime binding( aka late binding )polymorphism wherever overriding is allowed. However, with static methods, all the method calls are resolved and bound to the references at compile time( aka early binding).
IMO this is the difference between overriding and hiding.
HTH
Ajith
 
Velmurugan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriding and hiding are different.
1. Non-static methods are resolved at runtime, based on the real type of object. So a subclass non-static method with the same name and signature as a superclass non-static method overrides the superclass method.
2. Variables are resolved at compile-time, based on the type of the reference. So a subclass variable with the same name as a superclass variable hides the superclass variable.
3. Static methods are resolved and compile-time, based on the type of the reference. They don't participate in dynamic binding.
So a subclass static method with the same name and signature as a superclass static method hides the superclass method.
Hope this helps.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that helps a lot!!
 
Jatinip
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the latest tip from JDX that explains all:
http://developer.java.sun.com/developer/TechTips/2000/tt1010.html
You will need to scroll down a bit.
IHIH.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic