OverView
You can use val as the type of a local variable declaration instead of actually writing the type. When you do this, the type will be inferred from the initializer expression. The local variable will also be made final. This feature works on local variables and on foreach loops only, not on fields. The initializer expression is required.
val is actually a 'type' of sorts, and exists as a real class in the lombok package. You must import it for val to work (or uselombok.val as the type). The existence of this type on a local variable declaration triggers both the adding of the final keyword as well as copying the type of the initializing expression which overwrites the 'fake' val type.
WARNING: This feature does not currently work in NetBeans.
概述
你可以使用 val 声明本地变量的类型而不用写出它的实际类型,它实际的类型将由初始化的表达式推断。局部变量将被声明为 final 。这个功能只在局部变量和foreach循环中奏效,在字段中不奏效。并且初始化表达式是必须的。
val 实际上是实际的类型,它是一个真实的类并可以在 Lombok 的包中找到,你使用 val 前必须先导包。
警告:这个注解在 NetBean 中并不能正确的运行
综上所述:1:val Quote = new AnyObject(); [必须声明后面的表达式]
2:只能声明局部变量
3:声明出来的局部变量为 final 类型
由于这个功能不能再 IDEA 中正确运行,小伙伴们可以尝试一下在 Eclipse系列的产品中尝试运行一下
官网 Demo