12

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()

Community
  • 1
  • 1
n611x007
  • 8,952
  • 8
  • 59
  • 102

7 Answers7

11

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[]).

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
4

Simple Answer No, Reason, Specification is like that, JVM will only look for main and not any custom name as the starting point. It has to be called main with the exact signature public static void main(String[] args)

Logically also it makes sense how will JVM know that instead of main method, it has to look for boot or something else unless java command had an option to pass the start method.

But that is asking just too much for no good reason.

Secondly since its standardized it helps the developer community too, whoever looks at the code know how to run a given Java Standalone program, or say if you have got a project, your first point will always be look for the main method, and from there you move on.

mprabhat
  • 20,107
  • 7
  • 46
  • 63
  • It's good that it's standardized, but this should not be a showstopper as it still could have had a standardized a way to specify the name of the entry point. – n611x007 Jun 08 '12 at 11:25
  • One reason to do that would be better self-describing name for a central function. Although I admit it's arguable if main is not descriptive enough, and also we add that to it's overwhelmingly widespread usage. – n611x007 Jun 08 '12 at 11:29
  • as you correctly pointed out main is self describing too many flexibility becomes an overkill without much advantage gain in this case – mprabhat Jun 08 '12 at 11:31
2

No. You can not do that according to Java Language Specification. But if you want, as Java is a open source project, so download the complete source code of Java language and change it accordingly ( I mean change the source code of JVM itself). This is the only way you can do that.

So now you can understand, why I said it is impossible.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
2

At start JVM is looking from method public static void main with array of Strings as argument. So only thing you can do is rename argument args. If you want method like boot nobody stops you from doing something like this (personally I don't recommend that "pattern")

static void boot(String[] arguments){
    //your logic
}

public static void main(String[] args) {
    boot(args);
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • nice code example. :) I'm doing it like this currently. I just always felt that the name `main` is actually not very descriptive nowadays. Sounds more like a historical artifact. – n611x007 Jun 08 '12 at 11:06
  • I got used to `main` method, so for me `boot` is just one unnecessary method call. But every human is free so won't stop you :) – Pshemo Jun 08 '12 at 11:16
1

Your application starts running from public static void main(String[] args). It's like the point where JVM looks at to start the proceedings. If you change it, JVM will feel free to not start your application.

If you want to want to have boot() as the starting point of your application - call it in main().

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • Yes, calling `boot` is what I currently do. :) I just always felt that calling (ie. naming) it `main` is actually not a very descriptive name nowadays. – n611x007 Jun 08 '12 at 11:01
1

Simple answer is NO . When you start executing program it looks for public static void main(String[] args) which takes String array argument.From this entry point main thread get started.

amicngh
  • 7,831
  • 3
  • 35
  • 54
1

Yes,We can change the name of the main method if we can change the configurations of the JVM and let it look for a method with another name instead of main method.

pm dubey
  • 976
  • 11
  • 16