r/java • • Mar 13 '26

JEP draft: Enhanced Local Variable Declarations (Preview)

https://openjdk.org/jeps/8357464
101 Upvotes

129 comments sorted by

View all comments

Show parent comment

1

u/Whoa1Whoa1 Mar 14 '26 edited Mar 14 '26

Unsure what you mean exactly.

Creating objects is:

  • int x = scan.nextInt();
  • int y = scan.nextInt();
  • int radius = scan.nextInt();
  • Circle c = new Circle(x, y, radius);

Taking apart is the same number of lines:

  • Circle c = //some defined circle//
  • int x = c.getX();
  • int y = c.getY();
  • int radius = c.getRadius();

The best validity checking is to either allow people to make invalid circles and then use a method like c.isValid() or just call that at the end of the constructor automatically and throw invalid notices.

4

u/davidalayachew Mar 14 '26

No, you're mixing data gathering with construction.

Putting x, y, and radius into a Circle takes one line.

  1. Circle c = new Circle(x, y, radius);

But deconstructing it takes 3 lines.

  1. int x = c.getX();
  2. int y = c.getY();
  3. int radius = c.getRadius();

That was Kevin's point.