wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final in main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final in main method" Watch "final in main method" New topic

final in main method

preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
why does this code compile clean and run well(produces "positive infinity") when the main method has been declared final also???
class child1
public final static void main(String[] args){
double d = 10.0 / -0;
System.out.println("Positive infinity");
System.out.println("Negative infinity");
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Why did you expect declaring the method to be final to affect the output?? A final method can't be overriden but that doesn't alter the execution.
kapil apshankar
Ranch Hand

Joined: Dec 17, 2000
Posts: 66
Hi Preeti,
Declaring a method final will only mean that the method cannot be overriddden. The method will still execute as expected in the class where it is declared as final.

Hope this helps. Correct me if I am wrong.<p>Cheers <img src="smile.gif" border="0"> ,<br />Kapil
preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
hi all,
acc to my knowledge,the execution of a java app starts from the main method with the following syntax
public static void main(String arg[])
but if we make it final does that make it a different method or not ???
please explain
thank you
GanapathyRaman Natarajan

Joined: Jan 22, 2001
Posts: 5
hi Preeti
You are correct, that the enter point for a java application is the main method. But STATIC MODIFIERS ARE IMPLICITLY FINAL. The final keywords is used for methods. They cannot be overridden.
Similarly static method cannot be overridden.
So the final modifier along with the static modifier will not have any change in the main method. They are the same.
Please dont have a narrow notion that "public static void main(String args[])" is the entry point of the application. Just remember that the main method is the entry point.
You may ask that if static and final are same, then i can aswell declare the main method like this:
"public final void main(String args[])". The prg will compile, but will give you an error. Because the JVM check's for the main mehtod in the program along with the static modifier.
public static void main(String args[]) = public final static void main(String args[])
preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
thank you all , especially ganapathy for such a good explaination.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: final in main method
It's not a secret anymore!