Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In java, can "public static void main" be renamed or refactored?

I do not want to change the public static void ... String[] args part of the signature, but is it possible to "rename" this function (eg. just for fun)?

So the entry point for execution would be a function with another name.

Rename it to, like, boot (what would better reflect, if not being historical, the actual use for it in my particular case).


related

I'm interested in doing something different, but these questions are still interesting:

public static void main(String arg[ ] ) in java is it fixed?

Why the name main for function main()

like image 222
n611x007 Avatar asked Jun 08 '12 10:06

n611x007


People also ask

Can we change public static void main 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.

Can I rename main method in Java?

Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args) . You can only change the name of String array argument, for example you can change args to myStringArgs .

Why main () in Java is declared as public static void main What if the main method is declared as private?

The main method is public in Java because it has to be invoked by the JVM. So, if main() is not public in Java, the JVM won't call it. That's all about why the main method is declared public and static in Java.

Why we declare public static void main in Java?

The main method must be declared as static so that the JVM can access the main method directly by using the class name. When we execute a java program we use the class name so when we write static it will help the JVM to access the main method.


1 Answers

No. The Java Language Specification says:

A Java virtual machine starts execution by invoking the method main of some specified class, passing it a single argument, which is an array of strings.

The JVM Specification says the same thing:

The Java virtual machine then links the initial class, initializes it, and invokes the public class method void main(String[]).

like image 75
Oliver Charlesworth Avatar answered Oct 21 '22 09:10

Oliver Charlesworth