Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8858375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:48:53+00:00 2026-06-14T14:48:53+00:00

So the situation is something like this: private void myMethod() { System.out.println(Hello World); //some

  • 0

So the situation is something like this:

private void myMethod()
{
    System.out.println("Hello World"); //some code

    System.out.println("Some Other Stuff"); 

    System.out.println("Hello World"); //the same code. 

}

We don’t want to be repeating our code.

The technique described here works pretty well:

private void myMethod()
{
    final Runnable innerMethod = new Runnable()
    {
        public void run()
        {
            System.out.println("Hello World"); 
        }
    };

    innerMethod.run();
    System.out.println("Some other stuff"); 
    innerMethod.run(); 
}

But what if I want to pass in a parameter to that inner method?

eg.

private void myMethod()
{
    final Runnable innerMethod = new Runnable()
    {

        public void run(int value)
        {
            System.out.println("Hello World" + Integer.toString(value)); 
        }
    };

    innerMethod.run(1);
    System.out.println("Some other stuff"); 
    innerMethod.run(2); 
}

gives me: The type new Runnable(){} must implement the inherited abstract method Runnable.run()

While

private void myMethod()
{
    final Runnable innerMethod = new Runnable()
    {
        public void run()
        {
            //do nothing
        }

        public void run(int value)
        {
            System.out.println("Hello World" + Integer.toString(value)); 
        }
    };

    innerMethod.run(1);
    System.out.println("Some other stuff"); 
    innerMethod.run(2); 
}

gives me The method run() in the type Runnable is not applicable for the arguments (int).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:48:54+00:00Added an answer on June 14, 2026 at 2:48 pm

    Looks like you just want inner methods. Java does’t let you have them, so the Runnable hack you describe lets you sort-of declare an inner method.

    But since you want more control over it, why not define your own:

    interface Inner<A, B> {
        public B apply(A a);
    }
    

    Then you can say:

    private void myMethod(..){ 
        final Inner<Integer, Integer> inner = new Inner<Integer, Integer>() {
            public Integer apply(Integer i) {
                // whatever you want
            }
        };
    
    
        // then go:
        inner.apply(1);
        inner.apply(2);
    

    }

    Or use some library that provides functor objects. There should be many. Apache Commons has a Functor that you can use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My situation is something like this: class AbstractClass: def __init__(self, property_a): self.property_a = property_a
So I have something like this situation: $(document).on('click', 'a[data-link]', function () { var $this
When I'm writing code with QtCreator (2.4.1), I'm very often in this situation: something
I got a situation something like, i can use Private Frameworks of Apple in
I have a situation where I need to notify some users when something in
I have trouble when designing classes like this class C1 { public: void foo();
Suppose you have something like the following: class Shape // base class { private:
I have been googling for hours looking for something to handle my situation. I
Learning about notifyAll made me question something about notify: in a typical situation we
I have something like the following in the header class MsgBase { public: unsigned

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.