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; if(d == Double.POSITIVE_INFINITY) System.out.println("Positive infinity"); else System.out.println("Negative infinity"); } }
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12271
1
posted
0
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. Bill
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. Regards Kapil
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
posted
0
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 preeti
GanapathyRaman Natarajan
Greenhorn
Joined: Jan 22, 2001
Posts: 5
posted
0
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[]) IT DOES NOT CHANGE THE METHOD. HOPE THIS HELPS GANAPATHY
preeti dengri
Ranch Hand
Joined: Nov 30, 2000
Posts: 111
posted
0
thank you all , especially ganapathy for such a good explaination. preeti