9 ms·
Public static void main(String[] args) is dead
- abhiyerra 1y agoI learned to read line as the following in Java 1.2 so the Scanner class is new to me. :p BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String input = in.readLine();
- samlinnfer 1y agoJEP 445: Unnamed Classes and Instance Main Methods Released in Java 21 https://openjdk.org/jeps/445 https://openjdk.org/jeps/445
- rerdavies 1y ago:-) So not even remotely dead.
- dvdkon 1y agoIt's a shame that they only introduced this as a feature only for single-file programs. It would make Java a much better language in my eyes if it allowed package-level methods, or even multiple records/classes per file. But apparently "impact on how Java code is written in general" is not wanted.
- Sammi 1y agoPeople change their minds once they get used to something. There's reason to have patience.
- adgjlsfhk1 1y agoIt's amazing that ~30 years on, Java is finally becoming a not horrible programming language.
- vincnetas 1y agoMost likely you have not seen horrible languages or your bar for being not horrible is quite high.
- mrkeen 1y agoOr they've seen enough good languages to push Java down.
- adgjlsfhk1 1y agothis one. Java is far from the worst out there. I've used MuMPS and know of the horrors of TCL, but Java is once of those languages where everything takes 5x more more code than it should, and the result always feels ugly. part of my dislike is probably that I'm a math person, and the lack of operator overloading really makes any math not using the built-in types dreadful to read.
- bigstrat2003 1y agoJava was never a horrible programming language. The hate is wildly overblown.
- deleted 1y ago[deleted]
- userbinator 1y agoThe culture of insane overengineering around it is the problem, not the language itself.
- TZubiri 1y agoOnly bad experience I remember from Java was the int/Integer primitive/object issue and boxing. Also of course the misuse and overuse of classes when designing systems, but the complexity at design time is also the counterpart of the ease of use of well designed APIs. I remember using an IDE to write some code and it skipped the whole documentation and run/compile error phase, I could just cast mismatching types as needed. Probably C# and Swift/Objective C would only be above Java as top languages, being actually funded by paying customers instead of used by companies that depend on volunteer developed open source software and end-users that equate paying for software with evilness.
- Panzerschrek 1y agoSo, Java now supports free functions (not associated with some class). Or not really yet?
- vincnetas 1y agoI 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?
- throwaway47461 1y ago> What for would you use true free functions? The same things people use free functions for in any other programming language.
- rerdavies 1y agoCluttering up global namespaces? ;-P
- deleted 1y ago[deleted]
- Mawr 1y ago> What for would you use true free functions? I'm visibly shaking at this statement. What are you even saying here? A function is the most basic, fundamental unit of code organization. What Java does with its forced classes is a complete perversion of computation, sanity and anything reasonable.
- BinaryRage 1y agoNo, 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 https://openjdk.org/jeps/511
- TZubiri 1y ago
- yohannesk 1y agoWhy not abstract away the public static void main(String[] args) method with a top-level statement paradigm, similar to C#'s entry point simplification, to reduce boilerplate and enhance code conciseness?
- mhh__ 1y agoWhy should the entry point be a random special case? You're already admitting at this point that OOP is flawed so you might as well just have the balls to design a proper alternative (rather than a kludge)
- textlapse 1y agoA special static class that cannot be instantiated is not OOP already. C# does this well by letting you OOPfy your other code but doesn’t require you to use OOP for this monstrosity.
- deleted 1y ago[deleted]
- jayd16 1y agoIt always was and is a random special case.
- fhd2 1y agoThis pattern isn't OOP, there is no objects here. And Java generally isn't a good example of an OO language to me. It's more like Class Oriented Programming. That is mighty flawed but was for some reason quite fashionable. I wouldn't consider OO the way Ruby and other actually well designed languages do it flawed.
- cies 1y agoAnd Ruby is basically Smalltalk with a friendly syntax and lots of FP-goodness where it makes sense. Or, Ruby is a cute (mostly subset of) Perl. And after some Kotlin I must say: Kotlin is an acceptable "Ruby with static types".
- furyofantares 1y agoWe got there just after we got the ability to legitimately create useful small programs without understanding any of the thousands of lines of code.
- another_twist 1y agoThis is not a prank right ? Is it really gone ?
- bonzini 1y agoIt is, and it will be back soon just like Python's "if __name__ == '__main__'"...
- garbagepatch 1y agoIt makes sense to still have a main function. It doesn't make sense to have a class only to wrap that main function.
- nromiun 1y agoWhat? When did that go away? What is the alternative?
- rhdunn 1y agoIn a pyproject.toml file you can create/define scripts that reference an entry function. E.g.: # myapp/app.py def main(): print('Hello!') and in the pyproject.toml: [project.scripts] app = "myapp.app:main" You can still use the __name__ check at the end of the file, e.g.: if __name__ == '__main__': main() That way it works both as a standalone and when installed via pip/uv. -- Note: The scripts created by the installer are slightly more complicated versions of that __name__ check that handle the application argument (arg 0) and the exit code as the return value from main().
- rhdunn 1y agoIt's not gone per se -- you can still write out the long form version if you want (this is required for backward compatibility). What's now permitted is: 1. a simpler way of declaring a main method on a class -- you don't need the public or static, and the args is optional, so you can declare a `void main() { ... }` method on a constructable class; 2. declaring a main function (and other variables/functions) outside of a class -- here, the startup code will handle calling the main function, although I don't think the ability to call anonymous functions currently extends outside of the main function/file.
- throwmeaway222 1y agoMight come back to Java
- TZubiri 1y agoUgly? Sure. But isn't this the identity of Java? Taken away for almost no gain, there's literally no real issue stemming from requiring a verbose incantation to write a main routine, this is something you learn like day 1 on learning java, and it's something you write once in a blue moon, like 0.001% of the code you are going to write. It reminds me of the attempt at removing Richard Stallman from the FSF, yeah, you could do that and fix a problem, but then what you are left with isn't really the same thing as it was before, it's now actually the same soulless thing as the rest of the competing things. Saving grace is that obviously, you can still recite the incantation, it's not like public static void main(String[] args) is gone, just that you can skip it.
- yallpendantools 1y ago> this is something you learn like day 1 on learning java, and it's something you write once in a blue moon, like 0.001% of the code you are going to write. I think this is the exact audience for this change: for newbies in day 1 of 10 years in their programming journey. Less boilerplate to just swallow, less time spent debugging an otherwise-fine lab exercise if not for the wrong instantiation of Scanner, more time actually focusing on the basics they should be focusing on. Granted, I still think Java is a horrible first language to learn from or teach with but this is a step in the right direction. Might revisit this opinion in a few years, who knows. Oh also, we use Java in production so we also have a bunch of ad-hoc "scripts" written in Java for mostly one-off interactions. Nonetheless, these scripts can get unwieldy fast. You'd think I'm exaggerating the benefit here but I, for one, welcome one less level of indentation in these scripts.
- TZubiri 1y agoSure you could argue that it's better to learn java like this. But also you could argue that it was fine to learn java with an incantation. It's the first day of class, of course you are not going to understand everything you type. And you are getting exposure to important java concepts that you are going to circle back to later in your journey. Fwiw, other JVM languages like SCALA have already done this exact thing way before. But whatever, it isn't either good or bad. And I can still use the old way out of respect for tradition. To each their own
- cynicalsecurity 1y agoWhy did it suck exactly?
- hdjrudni 1y agoThat's what I wanted to know too, but all we get is this one line: > 1: When I was a Freshman in High School I asked a Junior what it meant. He had no clue. OK... so it's confusing for juniors. A lot of stuff is. I probably didn't know what it all meant when I started either, but so what? You copy and paste it and move on. Eventually it makes sense. Not a big deal.
- _moof 1y agoI don't understand how anyone ever didn't know what this meant. You have a class. It has a static method. The runtime starts your program by calling the method, passing it an array of command-line arguments. What's mysterious about this?
- maxlybbert 1y agoThe first book I read on Java managed to explain "public static void main(String[] args)" by the end of the first chapter. It wasn't a long chapter, either. I don't like Java, and the excessive boilerplate is a big part of that opinion. But it was never nearly as arcane as some people here are making it.
- hmry 1y agoThe fact that you have to explain what classes are, and what static methods are, a huge load of information, for a hello world program. And what's worse, it will leave students rightly confused about what the point of the class is, if it only has static members... Making it more likely they don't retain any of the information. Or alternatively, just say "type it exactly like this, don't think about what the magic words mean" which is a great way to ruin programming for people
- GaggiX 1y agoWhy there is a class in the first place
- crystal_revenge 1y agoOne thing I'll miss about this was the way this arcane writing increasingly made sense over time as you became a better programmer. I learned Java only after Python and I remember being not quite familiar with types so even the 'void' and 'String[]' where a bit mysterious. After learning basic types that part made sense. Then you start learning about Classes and objects, and you understood that main is a static method of this class Main that must be required somehow. As you dive deeper you start learning when this class is called. In a weird way what started as complete, unknowable boiler plate slowly evolved into something sensible as you began to understand the language better. I have no doubt that seasoned Java devs see a lot more in that invocation that I do. Good riddance though!
- panza 1y agoYeah, I agree with this sentiment. The boilerplate was oddly helpful when I was starting out.
- boxed 1y agoThat's just Stockholm Syndrome. Think about it: what more crazy garbage could you add that is useless for the beginner but "will make sense later"? The answer is: infinitely much. You could have to write each character in strings as separate characters for example. It would be absolute utter madness but it "would make sense later".
- almogo 1y agoI agree specifically, but disagree generally. As we become more and more acquainted with programming, a lot of what we do why we do it is incoherent nonsense to a beginner, but important nonetheless. The limit is actually your time and effort. But nobody likes to write optimal assembly by hand, the balance has to be struck somewhere.
- boxed 1y agoBut there is nothing important or useful in all that Java garbage. You could write entire programs without classes! That's the class, the static, AND the final keywords. You can't really escape String[], which imo is fine. It's better than argv/argc in C at least.
- Peteragain 1y agoWhat we seem to have forgotten is just how c like java was compared to other programming languages being advocated at the time. Objects made sense (remember Ada?) but c++ was a mess. Java was a breath of fresh air with lots of c legacy.
- vkazanov 1y agoOh, I do remember the narrative: OOP is amazing but Cpp is not pure OOP let's make the OOPiest language possible. I also remember casts all over the place in Java because polymorphism wasn't OOP enough. And then lambdas and functions were not OOP enough to be first class values and that's why we needed numerous "verb-classes" everywhere. And of course printf() is not OOP enough...
- cies 1y agoExactly. All wrong assumptions, and all needed to be tagged on to Java in later versions. I dont find Java's first versions well designed. The JVM was quite well designed, but the language not. And changing a popular language is hard. I think Java's dev do it very well (the process of changing it IS "well designed"). But still the language itself suffers a lot from the bad choices in the beginning. I like Kotlin: an OO language with as much FP in there as makes sense for an OO language.
- winrid 1y agoDid it slow down compile times at all to support two different file structures?
- deleted 1y ago[deleted]
- _b0t 1y agoNot understanding why having a well-defined function declaration "sucked", but alright, I guess! I mean even this part: ``` var name = IO.readln("What is your name? "); IO.println("Hello, " + name); ``` is _worse_. `IO.readln` doesn't tell you whether that's file I/O or stdin/out. The more explicit the better, if you ask me.
- DeathArrow 1y agoC# has top level statements since 4 years ago. Not only you don't need a class, but you don't even need a Main() method.
- DeathArrow 1y agoWhat I would like to see deprecated is over using objects and design patterns and SOLID and Uncle Bob principles, so we see less FizzBuzz Enterprise Edition and Java feels less as the Kingdom of Nouns.
- The_Fox 1y agoI distinctly recall early in my first university programming courses the instructor saying something to the effect of "Just wait until you learn 'public static void main'". I think we had only used BlueJ until that point in the course.
- giveita 1y agoLooks like C# to me
- kazinator 1y agoIf it is just syntactic sugar that has to create a class under the hood, it's just lipstick on a pig's turd. If you can't have other top-level functions in Java, then it's a special case, which is ugly.
- drewnoakes 1y agoC# has had top level statements since version 9.0 (Nov 2020), and it's still just a compiler trick that produces a static method behind the scenes. Top level functions work too, but in a similar way. Decompiled example: https://lab.razor.fyi/#41rAyMUVUJSfXpSYq5dcLDSRsbQ4My9dIbiyuCQ115qLyzk_rzg_J1UvvCizJNUnMy9VQykkv0AhJ7UsNUehuCSxJDU3Na9ESdOaiyskv8AHJOybWpKRn6IBEirLz0xRQBfnquZSUFBQwGtyLlgtyNhaL6bk4ihOjn-zzk9alSbAksAIAA https://lab.razor.fyi/#41rAyMUVUJSfXpSYq5dcLDSRsbQ4My9dIbiyu... > lipstick on a pig's turd There are several valuable compiler transformations that happen under the hood in languages like this. Closures as types, iterator/generator functions, async state machines. This is just another example.
- boxed 1y ago> If you can't have other top-level functions in Java, then it's a special case, which is ugly. I assumed this meant that you could have free functions, but you're saying you can ONLY have `main` as a free function? Ok, then I agree this is also garbage.
- tslater2006 1y agoFoesn't this show other top level functions besides main? From the JEP ```Top-level members are interpreted as members of the unnamed class, so we can also write the program as: String greeting() { return "Hello, World!"; } void main() { System.out.println(greeting()); }```
- veltas 1y agoI feel the same way about C/C++ having default return of 0 in main() only.
- kazinator 1y ago
- keithasaurus 1y agocould java please also learn that stand-alone functions are cleaner than static methods? even if it's just syntax sugar...
- fhd2 1y agoC++ calls them free functions, and I always found them rather elegant. A common pattern, at least a few years ago, was to use free functions for anything that'd be a method in Java, but doesn't actually require access to anything non-public.
- rottc0dd 1y agoFrom my previous comment in hn: As a java guy and think python is weird, I don't think this sucks. But, I also agree that can serve as terrible intro to programming if you start programming right away without understanding the basics of abstractions. But, often when we have tools either designed for a purpose in mind or a dominant paridigm or reaction to existing set of tooling, this can result in understandable yet extreme abstractions. Java is designed with OOP in mind and it kind of makes sense to have the user to think in terms of lego blocks of interfaces. Every method or class needs to have clear understanding of its users. public - software handle is for all users protected - software handle for current and extending classes default - software is exposed to current package private - software is restricted to be used in current class alone and nowhere else So, the beginning of java programming starts with interface exposed to the user or other programmers. Is it weird and extreme. Yes. At least, it is consistent.
- veltas 1y agoI agree, this is the whole point of "Hello world", which is to show the boilerplate required to start a program capable of outputting text, as well as the actual code that outputs that text. It's also the chance to get the build tools setup, people forget that some of the 'boilerplate' is the actions required to build, which often are much more involved for newer tools and frameworks. You can just say initially e.g. when I learned C++ that "#include <iostream>" can be "mostly ignored for now, but just know iostream stands for Input / Output stream, and this line is necessary to e.g. output with std::cout"; and there are no scars left from this.
- procaryote 1y agoSome scars... as cout << was always a bad idea, and taught people early on in their C++ development that being overly clever with operator overloading was expected
- veltas 1y agoWhich I think most people understood not to do themselves when they encountered their first issue with the precedence of << vs other operators in expressions they wanted to print.
- beginnings 1y agobut I need my public static void main arg arg arg
- emccue 1y agoMr Krabs!?
- charcircuit 1y agoIt hasn't been for over 20 years with Java 5. public static void main(String... args)
- Frieren 1y agoMy experience may be different from the rest of people at HN. But I am used to work in large projects that last decades. How main is written is totally irrelevant for my day to day work or my career. How does it affect you this change day to day?
- ertian 1y agoMany of us took programming 101 in Java and so typed this dozens of times without having a clue what it meant.
- mrkeen 1y agoNow we can type out the same semantics, and remain clueless about what it means, but with a new obfuscated syntax which stops us from asking about the semantics.
- derefr 1y agoStill for the better, because each token you don’t have to type when first learning programming is a token you can’t mistype. (Though the ultimate conclusion of this line of thinking is that programming 101 courses should be taught in as concise and syntax-light a language as possible, giving the learner as few opportunities to screw up the input as possible. I’m a fan of teaching programming in Ruby, personally. Not theory of programming, mind you; just programming as an iterative human process.)
- Frieren 1y ago> programming 101 courses should be taught in as concise and syntax-light a language as possible 100% this. To make Java be the all-language makes it a mess without a defined goal. It is better to start learning with a language better suited for it. And then the learner can specialize and expand to other languages. This also helps to create awareness that different languages have different use cases.
- tankenmate 1y ago
- flowerthoughts 1y agoThe unnamed class approach to global variables sounds nice. You can do hot-reload, but still have unencumbered syntax. OTOH, since Java files are named after their public class, it would have made more sense to just say the `public class X { }` wrapper is optional and only needed for attributes and inheritance. I don't quite understand why they need an anonymous class. > The Java compiler will compile that file to the launchable class file HelloWorld.class. In this case the compiler chooses HelloWorld for the class name as an implementation detail, but that name still cannot be used directly in Java source code. https://openjdk.org/jeps/445 https://openjdk.org/jeps/445
- derefr 1y agoWhat’s with Java devs and not including `import` lines in example code? Even here, when intending to demonstrate how dumb Java’s boilerplate is. But I notice it everywhere.
- andrewflnr 1y agoAmong other things, I think a lot of Java IDEs automatically manage imports anyway.
- derefr 1y agoI mean, the IDE can totally write out the import statements for you as you type and choose resolutions for identifiers; but if you paste in a random code snippet that references ambiguous identifiers from a third-party lib, no IDE can magically generate the import statements required to discriminate the correct resolutions. You have to go through and resolve the symbols yourself (or advance through them as error sites to get the class to compile.) Or, to put that another way: import statements aren’t pure boilerplate that should be thrown away; they encode real information (ambiguous symbol resolutions); and so, by eliding them from code examples, you’re discarding that information and forcing the learner to re-derive it. Which is especially bad when your code examples are referencing ambiguous undocumented static inner classes nested deep inside your library’s package namespace, where all the potential resolutions differ only by the particular package they exist in (since they’re all e.g. essentially batteries-included extensions of the base library implementing the same interface.) And where all this structure is as ugly as it is precisely because you didn’t expect the end-user of the library to need to understand it / interact with it. Yet your own [usually “advanced” or “feature demonstrating”] code examples force exactly that.
- TrianguloY 1y agoBecause usually you have an ide that manages imports for you automatically. So unless you are using a library that has multiple imports, usually you know the import to include, and in almost all cases it is automatically included. Btw: in the compact example! The IO class is implicitly imported, so that example truly works without any import at all!
- raverbashing 1y agoFunny how shaming and competition worked wonders for Java, instead of that bureaucratic mess that was first versions
- blobbers 1y agoIt looks more like C++ or C now. Never really became a java programmer. Hopefully never will have to.
- fhd2 1y agoBack in 2005 or so, Java felt like "C++, but we know better" to me. I had no choice but to use it, but boy did they omit some stuff that was actually useful. The boilerplate back then (driven by both the limitations of the language and a surge in architecture astronauts) was absolutely insane. Don't even get me started on anything labelled "Java Enterprise" in the 2000s, bloat, slowness and bugs everywhere. IBM, Oracle and friends were running quite the circus. Creating working, performant software in that environment was a brutal challenge. I think Java, the language, is only partly to blame, but it was limited enough to spawn pretty complex code generators and frameworks. Took the industy a decade or two to arrive at typed languages that were an actually good subset of C++ features. Java, in my opinion, wasn't one.
- macspoofing 1y agoWe're solving real problems now.
- sylens 1y agoLearning Java in high school, I loved how the very first lesson (focused on Hello World) had you type this out, only for the teacher to say “This will make sense later on but for now just type it in”.
- telcal 1y agoI haven't written Java since college CS, over 20 years ago, and I still remember that one line. It's burned in my memory.
- zuInnp 1y agoI don't get the hate ... yes, it is a lot of boilerplate, but at the same time it is very explicit and doesn't hide complexity.
- Yoric 1y agoEh. I used to teach Java as a first programming language and this always went "Just ignore the incantations for the time being, you'll learn more about them as time goes." This (and IO) was also one of the reasons for which my students tended to like OCaml much better than Java.
- gedy 1y agoReminds me when Google Translate came out about 20? years ago, I pasted in "public static void main (String[] args)" and out came in Spanish: "Secuencia pública de la cañería del vacío de los parásitos atmosféricos" (Public Sequence of the Vacuum Pipe of Atmospheric Parasites)
- cbeach 1y agoTimeline: Java 5 (2004) [code sample from the article]: public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("What is your name? "); String name = scanner.nextLine(); System.out.println("Hello, " + name); } } Scala 2 (2006): object Main extends App { val name = readLine("What is your name? ") println(s"Hello, $name") } Scala 3 (2021): val name = readLine("What is your name? ") println(s"Hello, $name") Java (2025) [code sample from the article]: void main() { var name = IO.readln("What is your name? "); IO.println("Hello, " + name); }
- lesser-shadow 1y ago"Hitler dead Wat nau?"
- keeda 1y agoThis hits home... literally. My high school kid had Python last year, and this year they started him on Java, and his very first reaction on coming back from school was, "WHY THE HECK DO I HAVE TO WRITE ALL THIS STUFF JUST TO PRINT HELLO WORLD??!?"
- theutopian 1y agoYou will not be missed!
- estimator7292 1y agoI'd rather have the old way. I've been explicitly turning this pattern off in C# for years because I find it much more annoying to have some random functions shat out into a file which is not a class. It makes so much less sense to have a special magical function you can put anywhere than an entire structured entry point.