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