10 Classic Java Main method face test _java

Source: Internet
Author: User

To share with you, if there is a mistake, please point out.

1. How do I define a class without the Main method?

No, there's no main method. We can't run Java classes.

Before Java 7, you could run Java classes by using static initialization. However, starting with Java 7 is not a viable option.

The parameter required by the 2.main () method is not an array of strings?

No, the argument for the main () method must be an array of strings.

However, when you introduce a parameter, you can pass the variable parameter of the string type as an argument to the main () method. The parameter must be an array.

Package Com.instanceofjava;
public class Mainmethod
{public
static void Main (String args[])
{
}
}

3. Can we change the return type of the main () method?

No, the return type of the main () method can only be null. Any other type is unacceptable.

Package Com.instanceofjava;
public class A
{public
static int main (string[] args)
{return
 1;  Run time Error:no Main method found
}
}

Why must the 4.main () method be static?

The main () method must be static.

If Main () allows non-static, the JVM will have to instantiate its class when the main method is invoked.

When instantiated, the constructor of the class is also invoked. If the constructor of this class has parameters, then there will be ambiguity.

For example, in the following program, what parameters does the JVM pass when instantiating class "a"?

Package Com.instanceofjava;
public class A
{public
mainmethod (int i)
{
//constructor taking one argument
} public
 void Main (string[] args)
{
//main method as Non-static
}

5. Can we declare that the main () method is Non-static?

No, the main () method must be declared static so that the JVM can invoke the main () method without instantiating its class.

If you remove the "static" declaration from the main () method, the compilation can still succeed, but it will cause the program to fail at run time.

Package Com.instanceofjava;
public class A
{public
void Main (string[] args)
{
System.out.println ("Indhu");     Run time error
}
}

6. Can we overload the main () method?

OK, we can overload the main () method. A Java class can have any number of main () methods.

In order to run the Java class, the main () method of the class should have a declaration such as "public static void Main (string[] args)." If you make any changes to this statement, the compilation can also be successful. However, you cannot run Java programs. You will get a run-time error because the Main method cannot be found.

Package Com.instanceofjava;
public class A
{public
static void Main (string[] args)
{
System.out.println ("Indhu");
 }
void main (int args)
{
System.out.println ("Sindhu");
}
Long main (int i, long d)
{
System.out.println ("Saidesh");
return D;
}
}

7. Can we declare that the main () method is private or protected, or that you do not need to access modifiers?

No, the main () method must be public. You cannot define the main () method as private and protected, nor do you need to access modifiers.

This is to enable the JVM to access the main () method. If you do not define the main () method as public, the compilation will succeed, but you will get a run-time error because the Main method cannot be found.

Package Com.instanceofjava;
public class A
{
private static void Main (string[] args)
{
//run time error
}
}

8. Can we overwrite the main method in Java?

No, you can't override the Main method in Java. This is because the main method is static and static methods are combined at compile time in Java, so you cannot override static methods in Java.

9. Can we end the main method in Java?

You can end the main method in Java. The JVM has no problem with this.

10. Can we synchronize the main method in Java?

Yes, the main method can be synchronized in Java, and the synchronized modifier is allowed in the declaration of the main method, so that the main method can be synchronized in Java.

The above is the entire content of this article, I hope to solve the Java Main method surface test questions help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.