{"id":445755,"date":"2024-02-21T19:04:24","date_gmt":"2024-02-21T18:04:24","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=idea&#038;p=445755"},"modified":"2025-02-25T13:58:28","modified_gmt":"2025-02-25T12:58:28","slug":"helloworld-and-main-meet-minimalistic","status":"publish","type":"idea","link":"https:\/\/blog.jetbrains.com\/fr\/idea\/2024\/02\/helloworld-and-main-meet-minimalistic","title":{"rendered":"Java 24: \u2018HelloWorld\u2019 and \u2018main()\u2019 meet minimalistic"},"content":{"rendered":"\n<p><i>(Originally published for Java 22, this post has been updated to include changes from Java 23 and 24.)<\/i><\/p>\n\n\n\n<p>&#8220;Just ignore the terms <code>class<\/code>, <code>public<\/code>, <code>static<\/code> and <code>args[]<\/code> for now; we&#8217;ll talk about it later&#8221;. If you have ever mentioned these lines to a new Java student, who is about to execute their first &#8216;HelloWorld&#8217; program, know that this is changing. If you are that student, well, congratulations, getting started is simpler in Java :-).<\/p>\n\n\n\n<p>Initially released as a preview language feature in Java 21, <a href=\"https:\/\/openjdk.org\/jeps\/495\" target=\"_blank\" rel=\"noopener\">Simple Source Files and Instance Main Methods<\/a> is currently being previewed for the fourth time in Java 24. This feature is for folks starting to learn Java. It simplifies the initial steps for students when they start learning basics, such as variable assignment, sequence, conditions and iteration. Students no longer need to declare an explicit class to develop their code, or write their <code>main()<\/code> method using this signature &#8211; <code>public static void main(String [])<\/code>, or use <code>System.out.println()<\/code> to output values to console. With this feature, classes could be declared implicitly, <i>the<\/i> <code>main()<\/code> method can be created with a shorter list of keywords and <code>println()<\/code>\/ <code>readln()<\/code> methods can be used to write to and read from console.<\/p>\n\n\n\n<p>In this blog post, I&#8217;ll cover why and how this feature is helpful for students using hands-on coding examples. I\u2019ll also cover some details and frequently asked questions relevant to educators or experienced programmers. Let&#8217;s get started.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Hi Students &#8211; Your HelloWorld code is changing<\/h2>\n\n\n\n<p><i>HelloWorld<\/i> refers to the first piece of executable code that anyone writes when they start learning a new language or a framework. It usually includes executing bare minimum code, such as printing &#8216;Hello World&#8217; on the console to ensure the setup is okay. In Java, it meant writing a class with a method <code>main()<\/code>. As the name suggests, <code>main()<\/code> is one of the most important methods. It defines the entry point of execution, when a class executes.<\/p>\n\n\n\n<p>What happens when you see that your &#8216;HelloWorld&#8217; code executed as expected? It brings a lot of joy and a sense of winning to the one executing it. I still remember the time when I was able to run my first piece of code in Java. I believe we kind of never forget our firsts, both in life, love and coding!<\/p>\n\n\n\n<p>Let\u2019s see how your HelloWorld code is changing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Class \u2018HelloWorld\u2019 before and after Java 21<\/h2>\n\n\n\n<p><i>Before<\/i> Java 21, you would need to define a class, say, <code>HelloWorld<\/code>, that defined a <code>main()<\/code> method with a specific list of keywords, to print any text, say, &#8216;Hello World&#8217; to the console, as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class HelloWorld {\n    public static void main(String[] args) {\n        System.out.println(\"Hello World\");\n    }\n}\n<\/pre>\n\n\n\n<p>With Java 21 this initial step was shortened and changed a little with Java versions 22, 23 and 24. Now (with Java 24), you can define a source code file, say, HelloWorld.java, with the following code, to print a message to the console (it doesn\u2019t need to define a class, it has a shorter signature for method <code>main()<\/code> and <code>System.out.println()<\/code> is shortened to <code>println()<\/code>):<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void main() {\n    println(\"Hello World\");\n}\n<\/pre>\n\n\n\n<p>The preceding code is simpler than what was required earlier. Let\u2019s see how this change could help you focus on what you need, rather than what you don\u2019t.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reducing cognitive complexity<\/h2>\n\n\n\n<p>One of the main benefits of the minimalistic <code>main()<\/code> method and not having to write an explicit class is &#8211; the code is concise and to the point. It introduces less concepts or keywords that could otherwise overwhelm new students. Here&#8217;s an image to compare the difference in the preceding code snippets:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/02\/compare-mains-1.png\" alt=\"\" class=\"wp-image-16062\"\/><\/figure>\n\n\n\n<p>Of course, you could ignore the terms <code>class<\/code>, <code>public<\/code>, <code>static<\/code>, <code>args[]<\/code>, <code>System.out<\/code> as suggested by your teacher, but they do use space in your memory every time you look at it. These little things pile up, in coding or life, overwhelming one. The minimalist <code>main()<\/code> and implicit enclosing class reduce cognitive complexity for you.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Compiling and executing your code<\/h2>\n\n\n\n<p>Once you are done writing your code, the next step is to execute it.<\/p>\n\n\n\n<p>On the command prompt, you could use just the <code>java<\/code> command to compile and execute this code (courtesy of <a href=\"https:\/\/openjdk.org\/jeps\/330\" target=\"_blank\" rel=\"noopener\">Launch Single-File Source-Code Programs<\/a>, since Java 11). Assuming you have defined your code in a source code file, say, HelloWorld.java, you could use the following commands to execute it:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">C:\\code\\MyHelloWorldProject\\java HelloWorld.java\n<\/pre>\n\n\n\n<p>For your quick information, since Java 22, it is possible to skip the compilation process multiple for code defined in multiple source code files too. Check out this <a href=\"https:\/\/openjdk.org\/jeps\/458\" target=\"_blank\" rel=\"noopener\">link<\/a> for details.\n\n\n\n<p>However, since &#8216;Simple Source Files and Instance Main Methods&#8217; is a preview language feature, you should add the flag <code>--enable-preview<\/code> with <code>--source 24<\/code> with these commands, as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">C:\\code\\MyHelloWorldProject\\java --enable-preview --source 24 HelloWorld.java\n<\/pre>\n\n\n\n<p>Sooner or later, you might switch to using an IDE to write your code. If you wish to use IntelliJ IDEA for creating instance main methods, here\u2019s a quick list of steps to follow. Create a new Java project, select the build system as IntelliJ (so you could use Java compiler and runtime tools), create a new file, say, HelloWorld.java with your instance main method and set the properties to use Java 24, before you run your code, as shown in the following gif (It could save you from typing out the compilation\/ execution commands on the command prompt each time you want to execute your code):<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/02\/new-proj.gif\" alt=\"\" class=\"wp-image-16062\"\/><\/figure>\n\n\n\n<p>Are you wondering if it would be better to create a &#8216;Java class&#8217; instead of a &#8216;File&#8217; in the &#8216;src&#8217; folder? The option of selecting a Java class would generate the body of a bare minimum class, say, <code>public class HelloWorld { }<\/code>. Since we are trying to avoid unnecessary keywords in the beginning, I recommended creating a new &#8216;File&#8217; which wouldn\u2019t include any code.<\/p>\n\n\n\n<p>If you plan to use IntelliJ IDEA, here are its configuration details. Java 24 support is available in <a href=\"https:\/\/www.jetbrains.com\/idea\/nextversion\/\" target=\"_blank\" rel=\"noopener\">IntelliJ IDEA 2025.1 EAP<\/a>. The final release of this version is planned for March 2025.<\/p>\n\n\n\n<p>In your Project Settings, set the SDK to Java 24. For the language level, select \u201824 (Preview) &#8211; Flexible consructor bodies, simple source files, etc.\u2019 on both the Project and Modules tab, as shown in the below settings screenshot:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/02\/proj-settings.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let\u2019s move beyond the simple \u2018Hello World\u2019 code and explore further.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What else can main() do apart from printing messages to the console?<\/h2>\n\n\n\n<p>It is an interesting question. Let\u2019s check out a few <a href=\"https:\/\/github.com\/malagupta\/HelloWorldProjJava24\" target=\"_blank\" rel=\"noopener\">code examples<\/a> below to learn more about it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1. Variable declarations, assignments and simple calculations<\/h3>\n\n\n\n<p>The initial steps in learning programming start with basics, such as declaring variables, assigning values, simple calculations and checking the values (by printing them to the console). Here\u2019s a common example that covers them all &#8211; calculating the total amount returned by a bank for a specified interest rate and principal amount. Let&#8217;s define this code in a source code file, say, CalcInterest.java:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void main() {\n    double principal = 17000;\n    double rate = 0.07;\n    double interest;\n\n    interest = principal * rate;\n    principal = principal + interest;\n\n    println(\"Interest            = \" + interest);\n    println(\"Amount after 1 year = \" + principal);\n}\n<\/pre>\n\n\n\n<p>In the next example, let\u2019s code something interesting &#8211; more than basic calculations, such as, printing a pattern .<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2. Print patterns, such as, big letters using a specified character<\/h3>\n\n\n\n<p>Here\u2019s another interesting problem that includes basics, such as, variable assignments, conditional statements (via <code>if-else<\/code>) and iteration (via <code>for<\/code>).<\/p>\n\n\n\n<p>The following code prints a letter P, using the letter X (as specified by the variable <code>charToPrint<\/code>), that is 10 lines in height (as specified by the variable <code>size<\/code>):<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void main() {\n    int size = 10;\n    char charToPrint = 'X';\n    for (int i = 0; i &lt; size; i++) {\n        for (int j = 0; j &lt; size; j++) {\n            if (j == 0 || (i == 0 || i == size \/ 2) &amp;&amp; j &lt; size - 1 || (j == size - 1 &amp;&amp; i &lt;= size \/ 2)) {\n                print(charToPrint + \" \");\n            } else {\n                print(\"  \");\n            }\n        }\n        println();\n    }\n}\n<\/pre>\n\n\n\n<p>The following gif shows the output of the preceding code:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/02\/print-pattern-letter-p.gif\" alt=\"\" class=\"wp-image-16062\"\/><\/figure>\n\n\n\n<p>Do you think you would be up for creating methods that could include logic to print other letters of the alphabet (A &#8211; Z), or even other alphabets (like the Devnagri)? Just asking :-)<\/p>\n\n\n\n<p>Before we move forward, did you know you could overload methods, that is, define multiple methods with the same name, such as, \u2018main\u2019 in your source code file? Which of these methods <code>main()<\/code> method would execute when you run your source code file? IntelliJ IDEA displays the run icon next to <i>the<\/i> <code>main()<\/code> method in your source code. The following gif shows how that icon moves when you change the signature of a <code>main<\/code> method:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/02\/which-main.gif\" alt=\"\" class=\"wp-image-16062\"\/><\/figure>\n\n\n\n<p>Let\u2019s take a step forward in the next example by animating words spanning multiple lines.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3. Animating multiline text &#8211; one word at a time<\/h3>\n\n\n\n<p>Soon, you\u2019ll find yourself using other concepts like, say, iterating words in a String, single and multiple line comments, using other classes from core Java API like Threads, perhaps also some exception handling. Here\u2019s an example of a multiline String that is displayed like animated text, one word at a time (all of which can be coded within the <code>main()<\/code> method):<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/**\n * The main method animates the display of lines of text by printing each word with a pause in between.\n * It splits the given text into individual lines, then splits each line into words.\n * It prints each word followed by a space, and pauses for a specified amount of time between each word.\n *\/\nvoid main() {\n    \/\/ Text to animate\n    String text = \"\"\"\n                  Why did the Java programmer bring a shovel to work? To 'dig' into the data structures!\n                  Why was the Java developer always so calm? Because they never lost their stack trace!\n                  Why did the Java developer go broke? Because he used up all his cache!\n                  Why did the Java developer always carry a pen? To 'interface' with their notes!\n                  \"\"\";\n\n    \/\/ Split into individual lines\n    String[] lines = text.split(\"\\n\");\n\n    \/\/ Loop through lines\n    for (String line : lines) {\n        String[] words = line.split(\" \");\n\n        \/\/ Loop through words\n        for (int i = 0; i &lt; words.length; i++) {\n\n\t        \/\/ Print the word\n            print(words[i] + \" \");\n            \/\/ Pause to create the animation effect\n            try {\n                Thread.sleep(200); \/\/ Change this number to modify word display speed\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }\n        \/\/ Start printing on the next line\n        println();\n    }\n}\n<\/pre>\n\n\n\n<p>The following gif shows the output of the preceding code:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/02\/animate-words.gif\" alt=\"\" class=\"wp-image-16062\"\/><\/figure>\n\n\n\n<p>If you are feeling adventurous, perhaps you could try other animations too &#8211; like animating each letter or decreasing the speed by which the words are printed to console. Are you up for this challenge :-)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4. Data structure problems<\/h3>\n\n\n\n<p>I\u2019ve seen that both students and experienced programmers enjoying solving problems that involve data structures. So, here\u2019s one for you (which can be coded using just the <code>main()<\/code> method): Given an array of integers, find the maximum subarray sum. The subarray must be contiguous, but could be of any length.<\/p>\n\n\n\n<p>Below is the code for your reference:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void main(String[] args) {\n    int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};\n    int maxEndingHere = nums[0];\n    int maxSoFar = nums[0];\n\n    for (int i = 1; i &lt; nums.length; i++) {\n        maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);\n        maxSoFar = Math.max(maxSoFar, maxEndingHere);\n    }\n\n    int maxSum = maxSoFar;\n    println(\"Maximum subarray sum: \" + maxSum);\n}\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example 5. Text based Hangman game<\/h3>\n\n\n\n<p>Here\u2019s another example that shows code for a text based hangman game &#8211; a simple game that asks you to guess one letter at a time. If the letter appears in the target guess word, it reveals itself in the word. This example uses the <code>Scanner<\/code> class from the Java API, which could be accessed either by using the full name while defining its variable, or by including an <code>import<\/code> statement outside the <code>main()<\/code> method (this helps you to get familiar with using concepts like importing other classes in an incremental way):<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void main() {\n    String[] words = {\"java\", \"programming\", \"computer\", \"algorithm\", \"developer\"};\n    String targetWord = words[(int) (Math.random() * words.length)];\n\n    StringBuilder guessedLetters = new StringBuilder(targetWord.length());\n    for (int i = 0; i &lt; targetWord.length(); i++) {\n        guessedLetters.append(\"_\");\n    }\n\n    int attempts = 6;\n    println(\"Welcome to Hangman!\");\n    println(\"Try to guess the word. You have \" + attempts + \" attempts.\");\n\n    while (attempts > 0) {\n        println(\"Current word: \" + guessedLetters);\n        String input = readln(\"Enter a letter: \");\n        char guess = input.charAt(0);\n\n        boolean found = false;\n        StringBuilder updatedWord = new StringBuilder(guessedLetters);\n        for (int i = 0; i &lt; targetWord.length(); i++) {\n            if (targetWord.charAt(i) == guess) {\n                updatedWord.setCharAt(i, guess);\n                found = true;\n            }\n        }\n\n        if (found) {\n            guessedLetters = updatedWord;\n            println(\"Correct guess! Updated word: \" + guessedLetters);\n        } else {\n            attempts--;\n            println(\"Incorrect guess! You have \" + attempts + \" attempts left.\");\n        }\n\n        if (guessedLetters.toString().equals(targetWord)) {\n            println(\"Congratulations! You guessed the word: \" + targetWord);\n            break;\n        }\n    }\n\n    if (attempts == 0) {\n        println(\"Game Over. The word was: \" + targetWord);\n    }   \n}\n<\/pre>\n\n\n\n<p>The following gif shows the output of the preceding code:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/02\/hangman-play.gif\" alt=\"\" class=\"wp-image-16062\"\/><\/figure>\n\n\n\n<p>Here\u2019s another challenge for you &#8211; I just realized that the input in the preceding code is case sensitive. It doesn\u2019t work if you type \u2018A\u2019 instead of \u2018a\u2019. Could you make it ignore the case of the letters that are typed?<\/p>\n\n\n\n<p>The intent with all these examples is that you could use minimum ceremonial code to start learning basics in Java.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Changing an implicit class to a regular class<\/h3>\n\n\n\n<p>When you are ready to level up and work with other concepts like user defined classes, you could also covert the implicit classes and code that we used in the previous examples, to regular classes, as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/02\/convert-to-reg-class.gif\" alt=\"\" class=\"wp-image-16062\"\/><\/figure>\n\n\n\n<p>In the next section, I\u2019ll answer a few questions that might seem more interesting for folks who are not new to Java and are interested in knowing more about this feature.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Some details and FAQs<\/h2>\n\n\n\n<p>Let\u2019s start by sharing that the name of this feature changed from its initial release, which is not common.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">A little bit of history<\/h3>\n\n\n\n<p>The name of this feature changed more than once since it was previewed in Java 21. This feature was introduced as <a href=\"https:\/\/openjdk.org\/jeps\/445\" target=\"_blank\" rel=\"noopener\">Unnamed Classes and Instance Main Methods<\/a>. Its name changed to <a href=\"https:\/\/openjdk.org\/jeps\/463\" target=\"_blank\" rel=\"noopener\">Implicitly Declared Classes and Instance Main Methods<\/a> in Java 22. There was no change to its name in <a href=\"https:\/\/openjdk.org\/jeps\/477\" target=\"_blank\" rel=\"noopener\">Java 23<\/a>. In Java 24, the name of this feature is changed again &#8211; <a href=\"https:\/\/openjdk.org\/jeps\/495\" target=\"_blank\" rel=\"noopener\">Simple Source Files and Instance Main Methods<\/a>.<\/p>\n\n\n\n<p>Changing name of a Java feature is not usual. It could happen if the changes in the feature no longer reflect the intention of its initial name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What do you mean by \u2018instance\u2019 main() method?<\/h3>\n\n\n\n<p>This feature refers to the main method as an instance main method since it doesn\u2019t use the keywords static with its declaration. In the past, the main method was defined using the static keyword, so that it could be accessed even without the need of creating any objects of the class in which it was defined.<\/p>\n\n\n\n<p>Behind the scenes, a source code file with an instance main method is compiled to a regular class with a no-arg constructor. Its instance main method is executed by creating an instance of this class, followed by calling main() on it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What happens when you create a source code file with method main(), but no class declaration?<\/h3>\n\n\n\n<p>Behind the scenes, the Java compiler creates an implicit top level class, with a no-argument constructor, so that these classes don\u2019t need to be treated in a way that is different to the regular classes.<\/p>\n\n\n\n<p>Here\u2019s a gif that shows a decompiled class for you for the source code file AnimateText.java:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/02\/decompile.gif\" alt=\"\" class=\"wp-image-16062\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Interacting with console &#8211; println() vs. System.out.println() calls<\/h3>\n\n\n\n<p><p>To make it simpler for new developers to interact with the console, that is, output messages to console and to read input from it, a new class &#8211; <code>java.io.IO<\/code> was created in Java 23. It contains just a handful of overloaded <code>readln()<\/code> and <code>println()<\/code> methods (as shown below):<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/02\/IO-class-struc.png\" alt=\"\" class=\"wp-image-16062\"\/><\/figure>\n\n\n\n<p>Class <code>java.io.IO<\/code> is automatically imported in implicit classes. So, instead of using <code>System.out.println()<\/code>, you can now use <code>println()<\/code> to output messages to a console (and <code>readln()<\/code> to read from console). Interestingly, <code>println()<\/code> was added to this class in Java 24.<\/p>\n\n\n\n<p>Whenever you are curious to find in which class or an interface a method is defined in, you can access this information by either using Quick Documentation (displays method and class details without switching to the source), or by using the Navigate feature in IntelliJ IDEA; navigating to method&#8217;s definition (as shown in the following gif):<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/02\/println-import.gif\" alt=\"\" class=\"wp-image-16062\"\/><\/figure>\n\n\n\n<p>Thanks to another newer feature, that is, <a href=\"https:\/\/openjdk.org\/jeps\/494\" target=\"_blank\" rel=\"noopener\">Module Import Declarations<\/a>, implicit classes automactically import all public classes exported by the module <code>java.base<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does this feature introduce a separate beginner\u2019s toolchain?<\/h3>\n\n\n\n<p>I\u2019m an educator and I fear that if we introduce simpler processes to students that are not mainstream, that could impact how they transition to learning other Java concepts later. I\u2019m glad to share that this feature doesn\u2019t do that.<\/p>\n\n\n\n<p>With implicit classes and instance main methods, students can still compile and run their code using the same tools and process, as are required when the size of their sample code or count for their source code files increase.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Variations of the main method in the implicit class<\/h3>\n\n\n\n<p>As we are aware, a method can be overloaded. Does that imply an implicit class can define multiple main methods? If yes, which one of them qualifies as the \u2018main\u2019 method? This is an interesting question. First of all, know that you can\u2019t define a static and non-static main method with the same signature, that is, with the same method parameters. The following method are considered valid main() methods in an implicit class:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static void main(String args[]) {}\npublic void main(String args[]) {}\npublic static void main() {}\nstatic void main() {}\npublic void main() {}\nvoid main() {}\n<\/pre>\n\n\n\n<p>If there is no valid main method detected in an implicit class, IntelliJ IDEA could add one for you, as shown in the following gif:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/02\/create-a-main-method.gif\" alt=\"\" class=\"wp-image-16062\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Educators could use this feature to introduce other concepts to the students in an incremental way<\/h3>\n\n\n\n<p>If you are an educator, you could introduce your students to other commonly used programming practices, such as creating methods- that is delegating part of your code to another method and calling it from the main method. You could also talk about passing values vs. variables to these methods.<\/p>\n\n\n\n<p>The following gif shows how to do it:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/02\/extract-method-inline-variables.gif\" alt=\"\" class=\"wp-image-16062\"\/><\/figure>\n\n\n\n<p>There are other practices too that I would have loved to mention like implementing the abstract methods of an interface, but, I think it would be better to leave them for another blog post.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preview Features<\/h2>\n\n\n\n<p>Simple Source Files and Instance Main Methods is in its fourth preview in Java 24. With Java\u2019s new release cadence of six months, new language features are released as preview features. They may be reintroduced in later Java versions in the second or more preview, with or without changes. Once they are stable enough, they may be added to Java as a standard language feature.<\/p>\n\n\n\n<p>Preview language features are complete but not permanent, which essentially means that these features are ready to be used by developers, although their finer details could change in future Java releases depending on developer feedback. Unlike an API, language features can\u2019t be deprecated in the future. So, if you have feedback about any of the preview language features, feel free to share it on the JDK mailing list (free registration required).<\/p>\n\n\n\n<p>Because of how these features work, IntelliJ IDEA is committed to only supporting preview features for the current JDK. Preview language features can change across Java versions, until they are dropped or added as a standard language feature. Code that uses a preview language feature from an older release of the Java SE Platform might not compile or run on a newer release.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Java language designers are reducing the ceremony that is required to write the first HelloWorld code for Java students, by introducing implicitly declared classes, instance main methods and shortened calls to <code>println()<\/code>\/ <code>readln()<\/code> to output messages and read data from the console. New students can start with bare minimum <code>main()<\/code> method, such as, <code>void main()<\/code> and build strong programming foundation by polishing their skills with basics like sequence, selection and iteration.<\/p>\n\n\n\n<p>Some folks might argue that this is a simple change, or that other languages already have it. For the first point, I\u2019d say, it is better to be late than never. Also, in life and coding, it is always better to focus on what you want to do, rather than focusing on what you don\u2019t want to. For the latter point, I\u2019d say, good for those languages :-)<\/p>\n\n\n\n<p>Stay tuned for my next blog post, in which I&#8217;ll cover more examples on creating scripts and utilities using implicit classes &#8211; for both new and experienced developers.<\/p>\n","protected":false},"author":921,"featured_media":549403,"comment_status":"closed","ping_status":"closed","template":"","categories":[4759,5088],"tags":[155,8703,8434,8704,8325],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/idea\/445755"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/idea"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/types\/idea"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/users\/921"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/comments?post=445755"}],"version-history":[{"count":9,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/idea\/445755\/revisions"}],"predecessor-version":[{"id":552957,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/idea\/445755\/revisions\/552957"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/media\/549403"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/media?parent=445755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/categories?post=445755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/tags?post=445755"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/cross-post-tag?post=445755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}