0

In my college, I was taught Java using BlueJ and we were asked to write main() in the following manner (no string array present as parameter):

public static void main()
{
//code
}

This works in BlueJ but it doesn't work in other IDEs like Eclipse (using right now).

All over the internet and books (except college texts), I find main() declared as public static void main(String[] args) .

My doubts:

  • When is the first way of declaring main() valid?
  • If the first declaration is wrong, why isn't BlueJ giving an error?
  • Is BlueJ designed to handle this internally?
  • What is the actual use of the string array parameter?
Mathews Mathai
  • 1,707
  • 13
  • 31
  • 1
    Possible duplicate of [Why main() method is needed in java main class](http://stackoverflow.com/questions/7443459/why-main-method-is-needed-in-java-main-class) – PM 77-1 Mar 08 '16 at 00:29
  • 4
    Your college is wrong. The actual signature is `public static void main(String[] args)`. The args array contain the command line arguments passed to your application. You should not use BlueJ, use IntelliJ or Eclipse (welcome to 2016). – Jazzwave06 Mar 08 '16 at 00:31
  • Is there a particular reason for why BlueJ is supporting the wrong syntax? – Mathews Mathai Mar 08 '16 at 00:32
  • You never noticed that BlueJ doesn't need a user defined 'main' method? And since it doesn't, why should it care about inproper signatures of the 'main' method? – Tom Mar 08 '16 at 00:32
  • 2
    @sturcotte06: In BlueJ's defense, it's a good beginner's IDE and doesn't immediately overwhelm the user. – Makoto Mar 08 '16 at 00:32
  • @Tom I read in one of the answers in link pasted by PM 77-1 that string array argumeng is required for the command line to pass info. Then how are the programs running without this requirement being satisfied? – Mathews Mathai Mar 08 '16 at 00:34
  • 1
    To clarify: the code you posted isn't invalid, it's just not a "real" `main` function. An IDE is allowed to give you more ways to run a piece of code, essentially be generating its own `main(String[])` and having it invoke the code you want to run. A lot of IDEs do that with unit test frameworks, for instance. – yshavit Mar 08 '16 at 00:35
  • @yshavit Now that makes sense.Thanks a lot. – Mathews Mathai Mar 08 '16 at 00:37
  • @MathewsMathai Now I wonder why you think that this isn't satisfied. Never thought about that BlueJ defines the 'main' method, so the user doesn't need to do that? This is one of the features of this IDE for beginners. – Tom Mar 08 '16 at 00:39
  • @Tom Thanks a lot. Got it now. would you mind converting that comment to an answer so that I could accept it and close the question? – Mathews Mathai Mar 08 '16 at 00:47
  • my guess is that BlueJ has its own legitimate main() or whatever that turns around and calls this bastardized main – Scott Sosna Mar 08 '16 at 02:17

1 Answers1

1

BlueJ does not require THE main method we all know aka public static void main(String[] args) { } so BlueJ is seeing that as a regular static method that happens to be called main "coincidentally."

Kevin Dixson
  • 419
  • 1
  • 4
  • 10