Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why I cannot have a method inside another method?

Reply
Thread Tools

Why I cannot have a method inside another method?

 
 
Shawn
Guest
Posts: n/a
 
      09-28-2006
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.


 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      09-28-2006

"Shawn" <(E-Mail Removed)> wrote in message
news:efhc23$tqn$(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
 
Furious George
Guest
Posts: n/a
 
      09-29-2006

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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why I can NOT have a class inside a method? Shawn Java 4 09-28-2006 06:17 PM
Access a control inside an usercontrol from another control inside another usercontrol nail ASP .Net 0 09-15-2004 03:55 PM



Advertisments