A def type declaration in groovy classes

Suddenly I realized that I can write

class Person {
     def String name
}

My question is: what is the difference between the above code and the classic one:

class Person {
   String name
}

Why does the first form even exist?

+1
source share
1 answer

There is no difference. Adding defa type definition to the beginning does nothing. However, this is permitted by the parser.

One way to test things like launch this groovyConsole, and start the AST browser (which is for the script)

class Person {
     def String name
}

Shows:

public class Person extends java.lang.Object { 

    private java.lang.String name 

}
+4
source

All Articles