Results 1 to 9 of 9
  1. #1
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Declaring a method inside a method: Runnable?

    Java Code:
    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
    
    }//sayHello() closes
    
    }//SomeClass closes here
    I have come across a blog in which it is claimed that using the above detailed technique on can run a method inside a method. Is this true. If it is can some take a moment to explain the code above.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What you should do is get a tutorial on threading, ..., and read it.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    What does "run a method inside a method" mean?

    This would be running a method inside a method, but I'm guesing that's not whatever blog post it is is actually saying:

    Java Code:
    public void myMethod() {
        myOtherMethod();
    }
    
    private void myOtherMethod() {
        Syetem.out.println("Look at me! Look at me!");
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Or is it "declare a method inside a method"? As the title says.

    In which case any anonymous class defined in a method would count presumably.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Or is it "declare a method inside a method"? As the title says.

    In which case any anonymous class defined in a method would count presumably.
    The entire question is moot; the ability to define a (local) class in the body of a method allows to define other methods inside a method body; have a look:

    Java Code:
    public static void main(String[] args) { // <-- an outer method
       ActionListener al= new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
             System.out.println("Who'd have thought it?");
          }
       };
       al.actionPerformed(null);
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    @JosAh

    You deserve some reputation for that

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    The entire question is moot; the ability to define a (local) class in the body of a method allows to define other methods inside a method body; have a look:

    Java Code:
    public static void main(String[] args) { // <-- an outer method
       ActionListener al= new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
             System.out.println("Who'd have thought it?");
          }
       };
       al.actionPerformed(null);
    }
    kind regards,

    Jos
    Which is an anonymous class...or have I mucked up my terminology...again?
    ;)

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Which is an anonymous class...or have I mucked up my terminology...again?
    ;)
    Nope, it's an anonymous local class and it's used in a bogus way, so it's a local anonymous bogus class; and it's extremely silly, so it's a local anonymous silly bogus class. ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by JosAH View Post
    Nope, it's an anonymous local class and it's used in a bogus way, so it's a local anonymous bogus class; and it's extremely silly, so it's a local anonymous silly bogus class. ;-)

    kind regards,

    Jos
    Local anonymous silly bogus class, now that deserves rep.

    @op: Do you understand your question better after there explanations?

Similar Threads

  1. Update Method inside a GUI?
    By Zepher in forum New To Java
    Replies: 8
    Last Post: 02-07-2011, 01:33 AM
  2. pass value inside method A to method B
    By masokis in forum New To Java
    Replies: 5
    Last Post: 09-01-2010, 04:06 AM
  3. how to return a value from runnable method
    By udinulis in forum AWT / Swing
    Replies: 10
    Last Post: 07-20-2010, 05:06 AM
  4. Unable to call paint() from inside method
    By Y. Progammer in forum New To Java
    Replies: 3
    Last Post: 03-01-2010, 12:47 PM
  5. Replies: 10
    Last Post: 02-28-2010, 01:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •