News for hackers. Stuff that matters.
Re:Java 22 Released by int_19h on Tuesday March 19, 2024 @03:34PM (#39759433)
They almost caught up with C#, which ditched requisite class declarations years ago. Although C# went one step further and allowed top-level code outside of methods, so that hello world is now:

   Console.WriteLine("Hello, world!")
Re:Java 22 Released by johnyzee on Tuesday March 19, 2024 @03:54PM (#39759618)
You can write write code outside of methods in a Java class, called an initializer block:

  class MyClass {
    {
      System.out.println("Hello World!");
    }
  }
This will run when an object of the class is instantiated. So not useful for the case in question (you could get rid of the class declaration but would presumably still need a main method).

(It's also pretty poor style, but occasionally useful in a pinch, like when instantiating an anonymous inner class, which has no constructors that take arguments)

Re:Java 22 Released by TillE on Tuesday March 19, 2024 @03:38PM (#39759468)
It has been a few years, but top-level statements in C# are still a fairly recent thing (C# 9 in 2020).