Hacker News new | ask | show | jobs
by int_19h 828 days ago
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!")
2 comments

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)

It has been a few years, but top-level statements in C# are still a fairly recent thing (C# 9 in 2020).