5

Is it syntactically correct to have a method inside the main method in Java? For example

class Blastoff {

    public static void main(String[] args) {

        //countdown method inside main
        public static void countdown(int n) {

            if (n == 0) {
                System.out.println("Blastoff!");
            } else {
                System.out.println(n);
                countdown(n - 1);
            }
        }
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
user1940007
  • 721
  • 3
  • 10
  • 14
  • It's not method inside methods, that's called recursion. And yes, your algorithm looks right. – Luiggi Mendoza Feb 23 '13 at 20:27
  • 11
    @LuiggiMendoza While `countdown` is recursive, it's also lexically nested inside `main`. –  Feb 23 '13 at 20:28
  • I am asking about the part where I have the countdown method in the main method, because my compiler says there is an error in that line – user1940007 Feb 23 '13 at 20:29
  • @delnan well, that's code problem. OP just needs to move the `countdown` function outside `main`. – Luiggi Mendoza Feb 23 '13 at 20:29
  • 2
    @LuiggiMendoza That's the question, duh! –  Feb 23 '13 at 20:29
  • @delnan well, it would be better to **post an answer** instead of wasting time in comments ;) – Luiggi Mendoza Feb 23 '13 at 20:30
  • 1
    @LuiggiMendoza I am not qualified to answer Java questions (see the answer by ruakh? I wouldn't have thought of anonymous inner classes). I am only qualified to point out when people miss the question ;-) –  Feb 23 '13 at 20:31

1 Answers1

7

No, not directly; however, it is possible for a method to contain a local inner class, and of course that inner class can contain methods. This StackOverflow question gives some examples of that.

In your case, however, you probably just want to call countdown from inside main; you don't actually need its entire definition to be inside main. For example:

class Blastoff {

    public static void main(String[] args) {
        countdown(Integer.parseInt(args[0]));
    }

    private static void countdown(int n) {
        if (n == 0) {
            System.out.println("Blastoff!");
        } else {
            System.out.println(n);
            countdown(n - 1);
        }
    }
}

(Note that I've declared countdown as private, so that it can only be called from within the Blastoff class, which I'm assuming was your intent?)

Community
  • 1
  • 1
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Yes that is my intent thanks. I am still getting the hang of the whole class/methods/objects structure what exactly does the `countdown(Integer.parseInt(args[0]));` line do? – user1940007 Feb 23 '13 at 20:35
  • 2
    @user1940007: The `args` argument to `main` is an array of the command-line arguments, as `String` instances. `args[0]` is the first command-line argument, as a `String`. [The `Integer.parseInt` method](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29) parses that `String` as a base-10 integer, and returns an `int`. So `countdown(Integer.parseInt(args[0]))` parses the first command-line argument as a base-10 integer, and calls `countdown` with it. (Try running your program as `java Blastoff 10`, and you'll see what I mean.) – ruakh Feb 23 '13 at 20:41