- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 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 Articles
- 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++
- Replacing ‘public’ with ‘private’ in “main” in Java
- Difference between “int main()” and “int main(void)” in C/C++?
- Differentiate between int main and int main(void) function in C
- C/C++ difference's between "int main()" and "int main(void)"
- Why main() method must be static in java?
- Why the main () method in Java is always static?
- Can we change return type of main() method in java?
- How to execute a static block without main method in Java?
- Why is the Main() method use in C# static?
- Can we make static reference to non-static fields in java?
- Can we change the access specifier from (public) while implementing methods from interface in Java?

Advertisements