7 ways of declaring main in java

  • Context: Java 
  • Thread starter Thread starter josephaswin
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
SUMMARY

There are seven recognized ways to declare the main method in Java, primarily by varying the access modifier and the static keyword. The two foundational declarations are public static void main(String args[]) and static public void main(String args[]). Other variations include using private or protected access modifiers, although these will compile but result in runtime errors due to the requirement for the main method to be public. Removing the static keyword will lead to compilation errors, confirming its necessity in the declaration.

PREREQUISITES
  • Understanding of Java access modifiers (public, private, protected)
  • Familiarity with Java method declarations
  • Knowledge of Java's main method requirements
  • Basic Java compilation and runtime error handling
NEXT STEPS
  • Research Java method overloading and overriding
  • Explore Java access modifiers in depth
  • Learn about Java runtime exceptions and error handling
  • Investigate the role of the static keyword in Java
USEFUL FOR

Java developers, computer science students, and anyone interested in understanding Java method declarations and their implications on program execution.

josephaswin
Messages
1
Reaction score
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
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?
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 2 ·
Replies
2
Views
2K