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 685335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:54:43+00:00 2026-05-14T01:54:43+00:00

Is there a way in java to do something like this: void fnc(void Reference_to_other_func());

  • 0

Is there a way in java to do something like this:

void fnc(void Reference_to_other_func());

What I’m trying is basically I have number of places where I need to display this same text to the user and the only difference is which method is invoked after this text. So for example instead of writing:

System.out.println("Hello");
f1();
//in some other place
System.out.println("Hello");
f2();
//etc

I would like to define one function:

public void f(void Reference_to_other_func())
{
System.out.println("Hello");
Reference_to_other_func();//HERE I'M INVOKING 
}

and then instead of repeating this whole code I could write something like this:

f(f1);
//in some other place
f(f2)
//etc.

Thanks for answers

  • 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-05-14T01:54:44+00:00Added an answer on May 14, 2026 at 1:54 am

    Java does not have first-class functions, which makes most functional-programming techniques somewhat tedious to implement.

    In this case, you can make an interface:

    public interface Callback {
        void doCallback();
    }
    

    (Edit: or you could use java.util.concurrent.Callable<V>, which allows you to specify a return type)

    then declare f to take an instance of this interface:

    public void f(Callback callback) {
        System.out.println("Hello");
        callback.doCallback();
    }
    

    and pass an instance of this interface to the function:

    f(new Callback() {
        public void doCallback() {
            f1();
        }
    });
    f(new Callback() {
        public void doCallback() {
            f2();
        }
    });
    

    As you can see, the gains are not going to become apparent unless you’re doing this quite a lot.

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

Sidebar

Related Questions

Is there a way to make a class function unoverriddable? Something like Java's final
I'm trying to do something like this: public void <String,int> getItem { return <Jen,23>;
Right now in some Java code I have something like this class A {
Is there a way to shorten type names in java. Something similar to typedef
Is there a way in java to have a ListModel that only accepts a
Is there any way to have a template (Java -> Editor -> Templates) in
Is it possible to achieve something like this in Java? My idea was to
When I compile something like this: public class MyClass { void myMethod(String name, String
I have Servlet that looks something like this: public class MyServlet extends Servlet {
Is there a way in Java to have a map where the type parameter

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.