The main() method inwards Java is starting signal of whatever standalone center Java application. JVM starts executing Java plan from principal method together with the thread which executes principal is called principal thread inwards Java. The principal method is too an of import theme inwards Java interviews for ii to iii years experienced developer. In this Java article, nosotros volition a span of questions related to the principal method inwards Java. Apart from Why principal is static inwards Java, I meet next questions dice on coming related to the principal method:
- Can nosotros overload the principal method inwards Java? Which principal method JVM volition call?
- Can nosotros override the principal method inwards Java?
- Can nosotros brand principal in conclusion inwards Java?
- Can nosotros brand principal synchronized inwards Java?
- How to telephone band a nonstatic method from principal inwards Java?
5 Main Method Interview Questions alongside Answers
Can nosotros overload principal inwards Java?
Yes y'all tin overload the principal method inwards Java, zip incorrect alongside this but Java volition precisely telephone band your specific principal method, i.e. principal method alongside the next signature:
public static void main(String[] args) or public static void main(String args...) which is the principal method every bit variable declaration method together with precisely supported post-Java five world.
Can nosotros override principal inwards Java?
No, y'all tin non override the principal method inwards Java, Why? because principal is a static method together with inwards Java static method is bonded during compile fourth dimension together with y'all tin non override static method inwards Java. If y'all declare a method alongside same cite together with signature its called method hiding.
Can nosotros brand principal in conclusion inwards Java?
Of course, y'all tin brand the principal method in conclusion inwards Java. JVM has no lawsuit alongside that. Unlike whatever final method, y'all tin non override principal inwards Java.
Can nosotros brand principal synchronized inwards Java?
Yes, principal tin hold upward synchronized inwards Java, synchronized modifier is allowed inwards the principal signature together with y'all tin brand your principal method synchronized inwards Java.
How to telephone band a nonstatic method from principal inwards Java?
This query applies non precisely to principal but all static methods inwards Java. Since nonstatic methods tin non hold upward called from static context directly, y'all demand to outset exercise an Object every bit local variable together with thence y'all tin telephone band nonstatic method using that object, every bit shown inwards the next example:
import java.util.Date;
/**
* Java plan to present how to telephone band non static method from static method inwards Java
*
* @author http://java67.blogspot.com
*/
public class StaticTest {
public static void main(String args[]) {
// calling non static method from principal inwards Java
//printCurrentTime(); //compile fourth dimension fault - tin non telephone band non static method from main
StaticTest seek = new StaticTest();
test.printCurrentTime();
}
public void printCurrentTime(){
System.out.println(new Date());
}
}
Output:
Tue November 06 19:07:54 IST 2012
/**
* Java plan to present how to telephone band non static method from static method inwards Java
*
* @author http://java67.blogspot.com
*/
public class StaticTest {
public static void main(String args[]) {
// calling non static method from principal inwards Java
//printCurrentTime(); //compile fourth dimension fault - tin non telephone band non static method from main
StaticTest seek = new StaticTest();
test.printCurrentTime();
}
public void printCurrentTime(){
System.out.println(new Date());
}
}
Output:
Tue November 06 19:07:54 IST 2012
Summary
- The main() method is a static method
- You tin overload main() method inwards Java.
- You cannot override main() method inwards Java
- You tin brand the principal method in conclusion inwards Java
- You tin brand the principal method synchronized inwards Java.
- You cannot telephone band a non-static method from principal inwards Java.
Other Java Interview questions y'all may like
0 Response to "Top five Java Main method Interview Questions amongst Answers"
Posting Komentar