--- title: Main() without Class (and it's not Kotlin) image: /assets/tunnel.jpg layout: post date: 2023-06-08 23:00:00 +0100 categories: - Java --- Jdk 21 has just reached [Rampdown Phase One](https://openjdk.org/projects/jdk/21/) and will be released with some awesome features such as [Virtual Threads](https://blog.devgenius.io/java-virtual-threads-715c162c6c39), sequenced collections, generational ZGC, and pattern matching. As if those aren't enough, Java is becoming more accessible to new programmers thanks to the JEP (Java Enhancement Proposal) [445: Unnamed Classes and Instance Main Methods](https://openjdk.org/jeps/445). In this short tutorial, we'll take a look at **_He-Who-Must-Not-Be-Named_** {% include image.html src="/assets/tunnel.jpg" alt="A tunnel" caption="Photo by Micaela Parente on Unsplash" %} Imagine learning the first concepts of Java without the burden of learning all the boilerplate needed to run a simple script that does the sum of two numbers. Thanks to this brand-new enhancement, It's now possible to write: {% highlight java %} void main() { int a = 1; int b = 2; int c = a + b; } {% endhighlight %} Instead of the [draconian](https://en.wikipedia.org/wiki/Draconian): {% highlight java %} public class MainClass { public static void main(String[] args) { int a = 1; int b = 2; int c = a + b; } } {% endhighlight %} But what does that _main()_ method standing alone means? Where does it reside? Is it possible to reference it from outside? First of all, let's make a distinction between anonymous classes ad unnamed classes. I think It's necessary considering how close these words are. **Anonymous** classes are a particular type of NESTED class used when we need to create a new class and reference it only once. A typical example we all have seen is with [Runnables](https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html): {% highlight java %} // Guess what, I have a name but I'm anonymous Runnable action = new Runnable() { @Override public void run() { System.out.println("RUN Forrest, RUN!!"); } }; {% endhighlight %} An unnamed class instead is a class that "simply" is not possible to reference by name from any other Class. Our _main_ method will be in fact placed **in an unnamed class, in an unnamed package, in an unnamed module**. The unnamed class will be only triggered if we run the program, so it has to contain a _main_ method. Additionally, as a consequence of being unnamed, it's impossible to extend it, it can't implement interfaces, can't define constructors, etc. Probably it's even an overkill calling it a class. Should be named unnamed script, really. Let's see a bigger example (remember to eventually run this code with the _"--enable-preview"_ flag): {% highlight java %} int zero = 0; int oneHundred = 100; int aNumber = 1234554321; void main() { assert xxxx(zero); assert !xxxx(oneHundred); assert xxxx(aNumber); } boolean xxxx(int x) { if(x < 0) { return false; } if(x < 10) { return true; } String y = String.valueOf(x); int half = y.length() / 2; for(int i=0; i < half; i++) { if(y.charAt(i) != y.charAt(y.length()-1-i)){ return false; } } return true; } {% endhighlight %} It's scripting made simple, with Java! This will be pretty awesome for testing with DataStructure and Algorithm as well. Just look at how many [public static void main(String[] args))](https://github.com/GaetanoPiazzolla/programming-dojo) I had to write! **PS**: While this seems to be a further step towards the "free functions" (functions outside classes) that Kotlin provides, this is completely outside the original idea of this JEP. If you feel depressed now, please remember that we Java programmers, do not need Kotlin. We have the Static keyword that makes us able to evade the burdens of Object-Oriented Programming by bypassing the concept of Class entirely, accessing a function without the need for a specific containing structure. The class, in this case, is simply the "namespace". **PS2**: How would you call that "_xxxx_" method up there? :) Thank you for reading!