- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can the main method in Java return a value?
The public static void main(String args[]) is the entry point of a Java program Whenever you execute a program the JVM searches for the main method and starts executing the contents of it. If such method is not found the program gets executed successfully, but when you execute the program it generates an error.
As the matter of fact you should declare the main method with public static as modifiers, void return type and String arguments if you change anything, JVM doesn’t considers as the entry point method and prompts an error at run time.
Therefore, you cannot change the return type of main method from void, at the same time you cannot return any value from a method with void type.
Example
public class Sample{ public static void main(String args[]){ System.out.println("Contents of the main method"); return 20; } }
Output
Sample.java:4: error: incompatible types: unexpected return value return 20; ^ 1 error
Therefore, you cannot return any value from main.
- Related Articles
- Can we change return type of main() method in java?
- Can we overload the main method in Java?
- Can we override the main method in java?
- Can we overload Java main method?
- Can we declare the main () method as final in Java?
- Can we create a program without a main method in Java?
- Can a method return multiple values in Java?
- Can we declare a main method as private in Java?\n
- Can we execute a java program without a main method?\n
- Can We declare main() method as Non-Static in java?
- Can we return this keyword from a method in java?
- Is main method compulsory in Java?
- Why the main method has to be in a java class?
- Why the main () method in Java is always static?
- What value does read() method return when it reaches the end of a file in java?

Advertisements