

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 we change the order of public static void main() to static public void main() in Java?
Yes, we can change the order of public static void main() to static public void main() in Java, the compiler doesn't throw any compile-time or runtime error. In Java, we can declare access modifiers in any order, the method name comes last, the return type comes second to last and then after it's our choice. But it's recommended to put access modifier (public, private and protected) at the forefront as per Java coding standards.
Syntax
public static void main(String args[]) { // some statements }
Example
class ParentTest { int age = 10; public int getAge() { age += 25; return age; } } public class Test { // Here we can declare static public void main(String args[]) static public void main(String args[]) { ParentTest pt = new ParentTest(); System.out.println("Age is: "+ pt.getAge()); } }
In the above example, we have declared static public main() instead of public static void main(), the code runs successfully without any errors.
Output
Age is: 35
- Related Questions & Answers
- What is the Eclipse keyboard shortcut for "public static void main(String[] args) " in Java?
- What is the difference between public, static and void keywords in C#?
- Can We declare main() method as Non-Static in java?
- Difference between void main and int main in C/C++
- Differentiate between int main and int main(void) function in C
- Why the main () method in Java is always static?
- Why main() method must be static in java?
- Can we change return type of main() method in java?
- Difference between “int main()” and “int main(void)” in C/C++?
- C/C++ difference's between "int main()" and "int main(void)"
- Why is the Main() method use in C# static?
- How to execute a static block without main method in Java?
- Can we overload Java main method?
- Can we overload the main method in Java?
- Can we override the main method in java?
Advertisements