Self-executing Java methods
Asked Answered
F

3

1

In JavaScript, it is possible to write a self-executing function like this:

(function foo() {
    console.log("bar");
}());

I'm looking to do this in Java. So for example:

// This code does not work obviously
public static void main(String[] args) {
    (foo() {
        System.out.println("bar");
    }());
}

Is there such a thing?

Fabe answered 18/6, 2014 at 16:34 Comment(2)
Why do you need a function here ? What's your goal ? Do you know that the scope works very differently in Java and JavaScript ?Overbold
If it makes you happy, it is possible to wrap code in their own blocks { }. So foo: { System.out.println("bar"); } works.Geostrophic
D
4

That javascript isn't really creating a "self-executing" function. It's defining a function, and then immediately executing it.

Java doesn't let you define standalone functions, so you can't do this in Java. You can however declare an anonymous class and immediately execute one of its methods:

new Runnable() {
  @Override
  public void run() {
    System.out.println("hello");
  }
}.run();

This is sometimes done with new threads. Something like:

new Thread(new Runnable() {
    // override Runnable.run
}).start();

(Though in a lot of cases, you'll want to do better thread management -- submit the runnable to an executor service, for instance.)

Debussy answered 18/6, 2014 at 16:40 Comment(4)
It's not an anonymous function, it's named foo.Fabe
@Fabe Good point, I'll edit. Can foo then be invoked later (ie, outside of its own scope)?Debussy
No, the name is just there for easier debugging purposes.Fabe
The name is often used in recursion too.Overbold
P
6

As others have said, there's not much reason to do this in Java, since the reasons for doing it in JavaScript aren't problems in Java. But you could do this in Java 8:

((Runnable)(() -> System.out.println("Hello, world"))).run();

which in essence is the same thing @yshavit's answer did in Java 7.

Puncheon answered 18/6, 2014 at 16:42 Comment(7)
here is a reason: private static final JAXBContext ctx = ((Supplier<JAXBContext>)(() -> { try { return JAXBContext.newInstance(""); } catch (JAXBException e) { throw new RuntimeException(e); } })).get();Neoimpressionism
@Neoimpressionism Good example, but ... Seems like there should be a cleaner mechanism for doing this, so that you could write something like private static final JAXBContext ctx = convertExceptionsToRuntimeException(() -> JAXBContext.newInstance(""));. I think a method like this could be written.Puncheon
Yeah. You need an UnsafeSupplier interface that allows Exception, and then a method to wrap that into a Supplier. And then you need that for all the functional interfaces you use. And then maybe sometimes you want to log and drop instead of throw. So the wrap method could take a Consumer<Exception>...Neoimpressionism
@Neoimpressionism I've just tried to write the method I was talking about, and ran into some nasty glitches in exception handling with lambdas that I wasn't aware of. What a mess.Puncheon
Ha. Yeah, I worked out in code the english I wrote. Checked exceptions really start sucking when you start working with the functional interfaces from the standard lib. I'll maybe throw it up on a gist later in case anyone is interested.Neoimpressionism
@Puncheon "since the reasons for doing it in JavaScript aren't problems in Java" - is this referring to namespace issues?Stiffler
@Abdul Sorry, you're asking me about something I said over 2 years ago. I don't remember what I meant, and I haven't been using Javascript much in that period.Puncheon
D
4

That javascript isn't really creating a "self-executing" function. It's defining a function, and then immediately executing it.

Java doesn't let you define standalone functions, so you can't do this in Java. You can however declare an anonymous class and immediately execute one of its methods:

new Runnable() {
  @Override
  public void run() {
    System.out.println("hello");
  }
}.run();

This is sometimes done with new threads. Something like:

new Thread(new Runnable() {
    // override Runnable.run
}).start();

(Though in a lot of cases, you'll want to do better thread management -- submit the runnable to an executor service, for instance.)

Debussy answered 18/6, 2014 at 16:40 Comment(4)
It's not an anonymous function, it's named foo.Fabe
@Fabe Good point, I'll edit. Can foo then be invoked later (ie, outside of its own scope)?Debussy
No, the name is just there for easier debugging purposes.Fabe
The name is often used in recursion too.Overbold
P
0

You can create helper methods (for e.g. run and get) that will execute your custom function.

use run if function doesn't return anything (side effects) and get otherwise

import java.util.function.Supplier;

public interface MyApp {

    static void run(Runnable supp) {
        supp.run();
    }

    static <R> R get(Supplier<R> supp) {
        return supp.get();
    }

    static void test() {

        run(() -> System.out.println("bar"));
        var v = get(() -> "Hello");

    }

}
Prostate answered 11/8, 2020 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.