The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Interview Questions On main() Method

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java Interview Questions On main() Method Posted: Aug 8, 2015 8:29 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java Interview Questions On main() Method
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1.Can we define a class without main method?
  • No, you can’t run java class without main method. 
  • Before Java 7, you can run java class by using static initializers. But, from Java 7 it is not possible.

2.Can main() method take an argument other than string array?
  • No, argument of main() method must be string array. 
  • But, from the introduction of var args you can pass var args of string type as an argument to main() method. Again, var args are nothing but the arrays.
  1. package com.instanceofjava;
  2. public class MainMethod
  3. {
  4. public static void main(String args[])
  5. {
  6. }
  7. }
3.Can we change return type of main() method?
  • No, the return type of main() method must be void only. Any other type is not acceptable.
  1. package com.instanceofjava;
  2. public class A
  3. {
  4. public static int main(String[] args)
  5. {
  6.  return 1;    //run time error : No main method found
  7. }
  8. }

4.Why main() method must be static?
  • main() method must be static.
  • If main() is allowed to be non-static, then while calling the main method JVM has to instantiate it’s class. 
  • While instantiating it has to call constructor of that class. There will be an ambiguity if constructor of that class takes an argument. 
  • For example, In the below program what argument JVM has to pass while instantiating class “A”?.
  1. package com.instanceofjava;
  2. public class A
  3. {
  4. public MainMethod(int i)
  5. {
  6. //Constructor taking one argument
  7. }
  8.  public void main(String[] args)
  9. {
  10. //main method as non-static
  11. }

5.Can We Declare main() Method As Non-Static?
  • No, main() method must be declared as static so that JVM can call main() method without instantiating it’s class. 
  • If you remove ‘static’ from main() method signature, compilation will be successful but program fails at run time.
  1. package com.instanceofjava;
  2. public class A
  3. {
  4. public void main(String[] args)
  5. {
  6. System.out.println("indhu");         //Run time error
  7. }
  8. }
6.Can We Overload main() method?
  • Yes, We can overload main() method. A Java class can have any number of main() methods. But to run the java class, class should have main() 
  • method with signature as “public static void main(String[] args)”. If you do any modification to this signature, compilation will be successful. 
  • But, you can’t run the java program. You will get run time error as main method not found.
  1. package com.instanceofjava;
  2. public class A
  3. {
  4. public static void main(String[] args)
  5. {
  6. System.out.println("Indhu");
  7.  }
  8. void main(int args)
  9. {
  10. System.out.println("Sindhu");
  11. }
  12. long main(int i, long d)
  13. {
  14. System.out.println("Saidesh");
  15. return d;
  16. }
  17. }
7.Can we declare main() method as private or protected or with no access modifier?
  • No, main() method must be public. You can’t define main() method as private or protected or with no access modifier. 
  • This is because to make the main() method accessible to JVM. If you define main() method other than public, compilation will be successful but you will get run time error as no main method found.
  1. package com.instanceofjava;
  2. public class A
  3. {
  4. private static void main(String[] args)
  5. {
  6. //Run time error
  7. }
  8. }

8.Can we override main in Java ?
  • No you can not override main method in Java, Why because main is static method and in Java static method is bonded during compile time and you can not 
  • override static method in Java. 
9.Can we make main final in Java?
  • you can make main method final in Java. JVM has no issue with that. Unlike any final method you can not override main in Java.
10.Can we make main synchronized in Java?
  • Yes, main can be synchronized in Java,  synchronized modifier is allowed in main signature and you can make your main method synchronized in Java.

Read: Java Interview Questions On main() Method

Topic: Java devs rejoice as private APIs stay in -- for now Previous Topic   Next Topic Topic: Application Performance Management solutions for the modern software-defined businesses

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use