bowbahdoe / mccue.dev-comments

0 stars 0 forks source link

pages/7-27-24-you-can-run-java-like-python-or-ruby #20

Open utterances-bot opened 2 months ago

utterances-bot commented 2 months ago

You can run Java like Python now

https://mccue.dev/pages/7-27-24-you-can-run-java-like-python-or-ruby

BoberMod commented 2 months ago

Can it work the same way as top-level statements in C#?

Is this a valid Java program?

System.out.println(Example.text());  

or

System.out.println("example");
bowbahdoe commented 2 months ago

@BoberMod Short answer, no.

Long answer, the feature that is going through preview right now is "Implicitly Declared Classes and Instance Main Methods."

So the first part of that is that you will be able to write a main class like this.

class Main {
    void main() {
        // ...
    }
}

Meaning that public, static, and String[] args are all optional. If your main method is non-static the runtime will basically do new Main().main()

The second part is for the entry-point into your program you can omit the class declaration.

void main() {
    // ...
}

This is equivalent to

class <UNNAMED> {
    void main() {
        // ...
    }
}

Which gets us most of the way there for the population most affected by needing a class declaration for main (beginners) but will only work for that entrypoint. You can't make a Book.java and omit the class declaration.

This is similar to how you can't import classes in the default/unnamed package from a named package. You can't reference an unnamed class from a named one.