• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final in main method

 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kapil apshankar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all , especially ganapathy for such a good explaination.
preeti
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic