Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Why I cannot have a method inside another method?

Thread view: 
Shawn - 28 Sep 2006 21:41 GMT
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.
Oliver Wong - 28 Sep 2006 23:02 GMT
> 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
Furious George - 29 Sep 2006 01:46 GMT
> Hi,
>
[quoted text clipped - 51 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.