Why I cannot have a method inside another method?

Discussion in 'Java' started by Shawn, Sep 28, 2006.

  1. Shawn

    Shawn Guest

    Hi,

    I am sorry for bothering you all. I ran into another question and
    solving this question by myself may take a long curve.

    I just realized that in Java, the following is not allowed:

    public void sayHello()
    {
    System.out.println("Hello World");

    void sayGreeting()
    {
    System.out.println("Good morning");
    }
    }

    This is inconvenient to me somehow. For example,

    public void sayHello()
    {
    System.out.println("Hello");
    System.out.println("Good morning");
    System.out.println("How are you?");
    ...//some code for something else

    //now again, it is tedious to retype the code
    System.out.println("Hello");
    System.out.println("Good morning");
    System.out.println("How are you?");

    }

    I hope to do:

    public void sayHello()
    {
    void sayGreeting()
    {
    System.out.println("Hello");
    System.out.println("Good morning");
    System.out.println("How are you?");
    }

    sayGreeting(); //1st time
    ...// code for doing something else
    sayGreeting(); //2nd time

    }

    But Java doesn't allow it. I know if I move up the method sayGreeting()
    by one level (outside of sayHello() ), things will be fine. But the
    problem is: if sayGreeting() is only useful for sayHello() and no other
    methods need or care about sayGreeting(), putting the sayGreeting()
    method in the class scope is not a good way. It clutters the class.

    Thank you very much for your help.
     
    Shawn, Sep 28, 2006
    #1
    1. Advertising

  2. Shawn

    Oliver Wong Guest

    "Shawn" <> wrote in message
    news:efhc23$tqn$...
    > Hi,
    >
    > I am sorry for bothering you all. I ran into another question and solving
    > this question by myself may take a long curve.


    Java doesn't allow it. I don't know why. You could package the method in
    a class as you've discovered earlier. I also heard that Sun is taking
    suggestions for features in Java 7, so you can submit this as a feature
    suggestion.

    - Oliver
     
    Oliver Wong, Sep 28, 2006
    #2
    1. Advertising

  3. Shawn wrote:
    > Hi,
    >
    > I am sorry for bothering you all. I ran into another question and
    > solving this question by myself may take a long curve.
    >
    > I just realized that in Java, the following is not allowed:
    >
    > public void sayHello()
    > {
    > System.out.println("Hello World");
    >
    > void sayGreeting()
    > {
    > System.out.println("Good morning");
    > }
    > }
    >
    > This is inconvenient to me somehow. For example,
    >
    > public void sayHello()
    > {
    > System.out.println("Hello");
    > System.out.println("Good morning");
    > System.out.println("How are you?");
    > ...//some code for something else
    >
    > //now again, it is tedious to retype the code
    > System.out.println("Hello");
    > System.out.println("Good morning");
    > System.out.println("How are you?");
    >
    > }
    >
    > I hope to do:
    >
    > public void sayHello()
    > {
    > void sayGreeting()
    > {
    > System.out.println("Hello");
    > System.out.println("Good morning");
    > System.out.println("How are you?");
    > }
    >
    > sayGreeting(); //1st time
    > ...// code for doing something else
    > sayGreeting(); //2nd time
    >
    > }
    >
    > But Java doesn't allow it. I know if I move up the method sayGreeting()
    > by one level (outside of sayHello() ), things will be fine. But the
    > problem is: if sayGreeting() is only useful for sayHello() and no other
    > methods need or care about sayGreeting(), putting the sayGreeting()
    > method in the class scope is not a good way. It clutters the class.


    Why worry about cluttering the class? Just make it private and the
    clutter is minimal.

    But if you insist on worrying about class clutter. You CAN put a
    method inside another method. Watch and learn.

    public class SomeClass {
    ....
    public void sayHello ( ) {
    final Runnable greeter = new Runnable ( ) { public void run ( ) {
    System.out.println("Hello"); System.out.println("Good Morning");
    System.out.println("How Are You"); } } ;
    greeter.run() ; // first time
    greeter.run() ; // second time
    }
    ....
    }


    >
    > Thank you very much for your help.
     
    Furious George, Sep 29, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nail
    Replies:
    0
    Views:
    1,558
  2. Shawn
    Replies:
    4
    Views:
    393
    Shawn
    Sep 28, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    917
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,033
    Smokey Grindel
    Dec 2, 2006
  5. Kiran More
    Replies:
    2
    Views:
    322
    Kiran More
    Nov 14, 2006
Loading...

Share This Page