Why I cannot have a method inside another method?

S

Shawn

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.
 
O

Oliver Wong

Shawn said:
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
 
F

Furious George

Shawn said:
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
}
....
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top