Download Main Method Description : TechnicalStack : http://technicalstack.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
This page was exported from TechnicalStack [ http://technicalstack.com ]
Export date: Sat Apr 29 20:47:42 2017 / +0000 GMT
Main Method Description
Explanation About main Method
Whether class contains main() method or not and whether main() method is declared according to requirement
or not these things wont be checked by compiler. At runtime JVM is responsible to check these things.
At runtime if JVM is unable to find required main() method then we will get run time exception saying “No
Such Method error :main.
For eg:Class Test
{
}
If we will compile this code by command javac Test.Java then it will be compiled properly but when we try to
run this code by command java Test we will get the run time exception saying “No Such Method error :main.
Syntax of main method
Public Static Void main(String args[])
{
}
Public: - To call by JVM from any where
Static: - Without existing object also JVM has to call this method and main method is not related to
any
object.
Void: - Main method will not return anything to JVM.
Main: - This is the name which is configured inside JVM
(String [] args):- command line argument.
The above syntax is very strict if we perform any change we will get runtime exception saying
“NoSuchMethodError:main”
Even though the above syntax is very strict the following changes are acceptable:1. The order of modifiers is not important that is instead of “public Static” we can take “Static Public” also.
2. We can declare “String[]” in any acceptable form:- main(sting[]args), main(sting args [])
3. Instead of args we can take any valid java identifiers
4. We can replace String[]with Var arg parameter
Main(String[]args) main(String….args)
5. We Can declare main() method with the following modifiers also:1. final 2. Synchronized 3. Stictfp
Class Test
{
Final Static Synchronized Strictfp Public Void main(string……usha)
{
System.out.println(“void main”);
}
}
After compiling (Javac Test.java) and running (Java test) the above example we will get the following result
Output:- Void main.
CaseI:- overloading of main method
Class Test
{
Public Static Void main(String[]args)
{
System.out.println(“String[]”);
}
// overloaded method
Public Static Void main(int[]args)
{
System.out.println(“int[]”);
}
}
Output:- string[]
overloading 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 then it will be executed just a normal method call.
Case 2:- Inheritance of main method
Inheritance concept is applicable for the main method. Hence while executing child class if child does not
contain main method then parent class main method will be executed.
Class parent
{
Public Static Void main(String[]args)
{
System.out.println(“parent class main method”);
}
}
Class Child extends Parent
{
}
If we compile this file and run we will get the following result
Javac Parent.java
Parent.class Child.class
Java Parent
Output:-parent class main method
Java Child
Output:-parent class main method
Case 3:- Overriding of main method
It seems overriding concept is applicable for main method but it is not overriding It is method hiding
Class parent
{
Public Static Void main(String[]args)
{
System.out.println(“parent class main method”);
}
}
Class Child extends Parent
{
Public Static Void main(String[]args)
{
System.out.println(“child class main method”);
}
}
If we compile this file and run we will get the following result
Javac Parent.java
Parent.class Child.class
Java Parent
Output:-parent class main method
Java Child
Output:-childclass main method
Note: - For main method inheritance and overloading concepts are applicable but overriding concept is not
applicable instead of overriding method hiding concept is applicable.
Post date: 2016-02-20 05:01:24
Post date GMT: 2016-02-20 05:01:24
Post modified date: 2016-02-20 05:10:51
Post modified date GMT: 2016-02-20 05:10:51
Powered by [ Universal Post Manager ] plugin. MS Word saving format developed by gVectors Team www.gVectors.com