Hacker News new | ask | show | jobs
by joachimma 796 days ago
> These include statements before super(…), which would give developers greater freedom in expressing constructor behavior – meaning string templates. This would make it easy to express strings that include values computed at run time – meaning scoped values. This would enable sharing of immutable data within and across threads; and implicitly declared classes and instance main methods.

I don't like the restriction on code before super. But I don't have a clue what they are referring to here. The code example doesn't use a template either.

  PositiveBigInteger(long value) {
    if (value <= 0)
      new IllegalArgumentException("non-positive value");
    super(value);
  }
4 comments

Once you call super, you invoke the base constructor. This can cause issues beca not all fields in the sub class has been set yet. Setting the fields before calling super prevents this issue.

Trivial method calls such as „toString“ can already cause issues and it’s worse once you want to guarantee certain value types properties.

The intention for "nothing before super" is clear, but that rule never delivered on its promises. Because all those footguns are still in place, in field initializers as well as in function calls inside the super() argument list:

  class Sub extends Super {
    int field = footgunA();
    Sub() {
      super(footgunB());
    }
  }

It's a zero merit headache factory and good intentions alone cannot change that.
As far as I know `footGunA` can only be a static method and cannot leak `this`.

This is perfectly fine as far as I'm concerned.

I should have checked instead of replying from memory: It's actually footgunB that is refused by the compiler. footgunA is accepted and will even call happily into an overridden implementation from a field initializer in the superclass. That way you can get a method to see a final field null on one call and non-null on the next.
That paragraph appears to be plagiarized from an Infoworld article [0] but with nonsensical changes to the punctuation (e.g. some of its semicolons changed to "– meaning"), which is why it makes no sense.

[0] https://www.infoworld.com/article/3714933/jdk-23-the-new-fea...

I agree that the wording of that was really weird. String templates aren't really the first thing that comes to mind when thinking of "freedom in expressing constructor behaviour".
Idk why you got downvoted. Clearly this feature is not about "string templates" -- it's very generic.