▲ | Panzerschrek a day ago | |||||||||||||||||||||||||||||||
So, Java now supports free functions (not associated with some class). Or not really yet? | ||||||||||||||||||||||||||||||||
▲ | happymellon a day ago | parent | next [-] | |||||||||||||||||||||||||||||||
They are associated with a class, but the class is basically the file. There are limitations by doing it this way but it is out of preview and officially supported as of Java 25. | ||||||||||||||||||||||||||||||||
▲ | ninjin a day ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||
If so, are we observing a move towards Universal Suffrage in the Kingdom of the Nouns? [1] [1]: https://steve-yegge.blogspot.com/2006/03/execution-in-kingdo... | ||||||||||||||||||||||||||||||||
▲ | BinaryRage a day ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||
No, these are just default imports. Module import declarations are in for JDK 25 to make this kind of thing even easier: https://openjdk.org/jeps/511 | ||||||||||||||||||||||||||||||||
▲ | vincnetas a day ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||
I think all functions are associated with some class. But these classes sometimes are hidden from you and you don't need to know about them. Does that help you? What for would you use true free functions? | ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||
▲ | naruhodo a day ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||
Nope. It's just sugar. [1] It's somewhat implicit in the JVM spec, but functions are always bound to classes as methods. [2] The only kinds of heap objects you can have a reference to are class instances and arrays. [3] The lack of free functions is really only a minor inconvenience for language implementers. Clojure, for instance, just compiles new classes to hold functions as methods. From a programmer's perspective, you can write lambdas, but again, it's just syntactic sugar over an inner class. [1] https://www.happycoders.eu/java/main-method/ [2] https://docs.oracle.com/javase/specs/jvms/se23/html/jvms-4.h... [3] https://docs.oracle.com/javase/specs/jvms/se23/html/jvms-2.h... | ||||||||||||||||||||||||||||||||
▲ | TZubiri a day ago | parent | prev [-] | |||||||||||||||||||||||||||||||
Presumably these exist in a Main.java file and the java class is implied. Similarly the string params are also implied, and the non param main() function is just an alias or a wrapper for main(String). It's just syntactic sugar. |