class Test{
public static final void main(String args[]){
}
}
What are the other access modifiers can use with main() in Java 1.8?
class Test{
public static final void main(String args[]){
}
}
What are the other access modifiers can use with main() in Java 1.8?
Yes you can, but final
does not make sense when used with a static method, since static methods cannot be overridden anyway.
By the way, final and static are not access modifiers. Access modifiers control which entity is allowed to access a method/field.
You can use the final
modifier. You can see that by simply compiling the program that you gave in your question.
The Java Language Specification has this to say about the final
keyword as applied to methods:
A method can be declared final to prevent subclasses from overriding or hiding it.
That is, if the method is an instance method (not static), it can be overridden, and final
will prevent that. If a method is static, it can be hidden, and final
will prevent that.
final
does not affect the method because it is already declared static
, and hence it will not affect the program in any way. It only means that you cannot hide this method in a subclass.
Therefore if you wanted to make a class
class Hello2 extends Test {
....
}
you cannot make a public static void main(String args[])
function in this class.
A static
method can be final
. As specified in the Java Language Specification, section 8.4.4.3, when a static method is final, it means that a subclass cannot "hide" it, i.e., define a method with the same name and same signature.
So, you can use final
on your main
method. The method will be usable as the main entry point of your program. Additionally, subclasses or your main class will not be able to hide it.