message = 'Hello' private String name Hello(String name) { this.name = name } String greet() { return "$message, $name speaking" } } Methods and classes are public by default
name Hello(String name) { this.name = name } String greet() { return "$message, $name speaking" } } Fields and static fields require visibility You created both an instance property and a static property!
private String message = 'Hello' private String name Hello(String name) { this.name = name } String greet() { def result = "$message, $name speaking" return result } } But it’s fine to use ‘def’ for local variables
{ String name int age } Person.builder() .name('Guillaume') .age(37) .build() Different strategies available: chained setters, external builder class, type-safe initializer
"number 123"; break case "abc": "string abc"; break case String: "is a string"; break case [1, 2, 3]: "in list"; break case ~/.*o+.*/: "regex match"; break case { it < 3 }: "closure criteria"; break default: "unknown" }
if (order.getCustomer() != null) { if (order.getCustomer().getAddress() != null) { System.out.println(order.getCustomer().getAddress()); } } } // Groovy println order?.customer?.address Also give more meaningful NPEs!