7 ways of declaring main in java

  • Java
  • Thread starter josephaswin
  • Start date
  • Tags
    Java
In summary, the conversation discusses various ways to declare the main method in Java, including using different keywords such as private or protected. However, the code will only compile if the main method is declared as public and static. The reasons for wanting to declare main() in different ways are also questioned.
  • #1
josephaswin
1
0
hi,

i know there are 7 ways to declare main in java
as

1.public static void main(String args[])
2.static public void main(String args[])

but what are the other ways?

but only change first two keywords
i.e. public and static
and use the required keywords.

this is the question given to me.
 
Technology news on Phys.org
  • #2
Apparently you can declare main() as private or protected, too. The code will compile, but you'll get an error when you run the program saying main() isn't public. I didn't know that. Also, I don't think you can remove "static" as you'll get an error when you try to compile if you do. Why do you want to declare main() in different ways anyway?
 
  • #3


Hi there,

Yes, there are a total of 7 ways to declare the main method in Java. The other 5 ways are:

3. public static void main(String[] args)
4. static void main(String args[])
5. public static void main(String... args)
6. static public void main(String[] args)
7. public static void main(String[] args) throws Exception

In these ways, the first two keywords, i.e. public and static, are changed and the required keywords are used. It is important to note that the main method in Java should always be declared as public, static, and void. However, the order of the keywords can be changed as long as all three are present. The last way also includes an exception, which is used to handle any errors that may occur in the main method.

I hope this answers your question. Let me know if you have any further queries. Happy coding!
 

1. What is the purpose of declaring a main method in Java?

The main method in Java serves as the starting point for a Java program. It is the method that is executed first when a Java program is run and it is responsible for controlling the flow of the program.

2. How many ways can a main method be declared in Java?

There are seven ways to declare a main method in Java, including the traditional "public static void main(String[] args)" method, as well as alternative methods such as "public static void main(String... args)".

3. What is the difference between declaring a main method with "String[] args" and "String... args"?

The only difference is in the number of arguments that can be passed to the main method. "String[] args" allows for only one argument to be passed, while "String... args" allows for multiple arguments to be passed as an array.

4. Can a main method be declared as private in Java?

No, a main method in Java must be declared as public in order for it to be accessed by the Java Virtual Machine (JVM). Declaring it as private will result in a "Main method not found" error when attempting to run the program.

5. Is it necessary to declare a main method in every Java class?

No, it is not necessary to declare a main method in every Java class. A class without a main method can still be executed if it is called by another class with a main method. However, if a class is meant to be run as a standalone program, it must have a main method declared in it.

Similar threads

  • Programming and Computer Science
Replies
3
Views
772
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
625
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
749
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top