Main ():-
Weather class contains main ()
method or not and weather main () method is declared according to requirement
or not these things won’t be checked by compiler at runtime JVM is responsible
to check these things if JVM unable to find main () method then will get
runtime exception saying NoSuchMethodError: main
At runtime JVM always searches
for the main () method with the following prototype
public static void main ( String
[] args)
public -à call by JVM from any where
static - > without existing
object also JVM has to call this method
void -> main () method won’t
return anything to JVM
main -> This is the name which
is configured inside JVM
String [] args -> command-line
arguments
The above syntax is very strict
and if we perform any change then we will get RuntimeException saying
NoSuchMethodError: main
Even though above syntax is very
stick the following changes are acceptable .
Instead of public static we can
take static public i.e the order of modifier is not important
We can declare string [] in any acceptable
form
main (String [] args)
main (String []args)
main( String args[])
instated of args we can take an
valid java identifiers.
Ex:- main(String[] raj)
We can replace String[] with
var-args parameters.
Ex :- main(String … args)
We can declare main () method
with the following modifiers
Final,synchronized,strictfp
Class Test{
Static final syschronized
strictfp public void main( String… raj){
System.out.println(“valid main
method”);
}
}
Which of the following main ()
method declarations are valied
public static void main(String args){
//wrong
}
public static void Main(String [] args){
//wrong
}
public void main(String[]
args){
//wrong
}
public static int main(string[]
args){
//wrong
}
final synchronized strictfp public void main(String[]
args){
//wrong
}
final synchronized strictfp public static void main(String[]
args){
//currect
}
public static void main(String[]
args){
//currect
}
In which of the above cases we will get
compile time error we won’t get runtime saying NosuchMethodError : main
Case
1:-
Overriding of the main () method is possible but JVM will always
call String [] argument main () method only. The other overloaded method we
have to call explicitly like normal method call.
Ex :- package com.rg.test;
public class Test {
public static void main(String[] args) {
System.out.println("string[]");//string []
}
public static void mian(int [] args){
System.out.println("int[]");
}
}
Case 2:-
Inheritance concept applicable
for main () method hence while executing child class if child doesn’t contains
main () method then present main () will be executed
Ex:- package com.javahighq.test;
public class Test {
public static void main(String[] args) {
System.out.println("string[]");//string []
}
}
class JavaProj extends Test{
}
Case
3:-
package com.javahighq.test;
public class Test {
public static void main(String[] args) {
System.out.println("parent
main");
}
}
//It is method hiding but not overriding
class JavaProj extends Test{
public static void main(String[] args) {
System.out.println("child
main");
}
}
It seems overriding concepts applicable
for main () method but it is not overriding method hiding concept is
applicable.
Note:-
For main () method inheritance &
overloading concepts are applicable but overriding concept is not applicable
instead of overriding method hiding concept is applicable.
No comments:
Post a Comment