r/java Sep 19 '18

JEP draft: Concise Method Bodies

http://openjdk.java.net/jeps/8209434
62 Upvotes

51 comments sorted by

View all comments

10

u/dpash Sep 19 '18 edited Sep 19 '18

While this will make getters much concise, it won't help much with setters, because they're a statement.

private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

would become

private String name;
public String getName() -> name;
public void setName(String name) -> this.name = name;

I'm curious to see how their introduction in C# changed the language. My initial feeling is that the usefulness of this change might be of limited scope, if only because only the most basic of methods would benefit from this.

Edit: I originally had the following setter

public void setName(String name) -> { this.name = name; }

but it was pointed out I had needless braces.

7

u/lukaseder Sep 19 '18

FTFY:

record Foo(String name);

4

u/dpash Sep 19 '18 edited Sep 19 '18

If and when we get data classes. Also, if and when we get this JEP :)

3

u/lukaseder Sep 19 '18

FTFY, current day Java:

public class Foo {
  public String name;
  public Foo(String name) { this.name = name; }
}

4

u/dpash Sep 19 '18 edited Sep 19 '18

From what I remember of the data class document, the fields don't quite have public access, but can be accessed like they do. I think it was possible to create a custom setter or getter if you need to customise it.

4

u/lukaseder Sep 19 '18

99.9% YAGNI

1

u/dpash Sep 19 '18

I do like C#'s properties.