*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confused about final static public void main(String []arguments) 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confused about final static public void main(String []arguments)" Watch "confused about final static public void main(String []arguments)" New topic
Author

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

Ira Jain
Ranch Hand

Joined: Sep 06, 2000
Posts: 70
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

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Aug 07, 2000
Posts: 30
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

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Aug 26, 2000
Posts: 10065
Hi Bill!
here you can see what will happen if you attempt to "override" static method...


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Mar 17, 2000
Posts: 5782
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


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Velmurugan
Greenhorn

Joined: Oct 11, 2000
Posts: 5
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

Joined: Jun 30, 2000
Posts: 1070
Thanks, that helps a lot!!
Jatinip
Greenhorn

Joined: Oct 11, 2000
Posts: 2
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused about final static public void main(String []arguments)
 
Similar Threads
equals
String.equals
Transient & Static Variables
A few questions, any takers
synchronization--->JQ+