Java中Lombok @Value注解導(dǎo)致的variable not been initialized問題
想要修改一個(gè)POJO類,在其中增加一個(gè)字段,但是增加以后就開始報(bào)錯(cuò):
/** * Generates a lot of code which fits with a class that is a representation of an immutable entity. *<p>* Equivalent to {@code@Getter @FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE) @AllArgsConstructor @ToString @EqualsAndHashCode}. *<p>* Complete documentation is found at<a href='http://www.gepszalag.com/bcjs/<https://projectlombok.org/features/Value>' rel='external nofollow' >the project lombok features page for@Value</a>. * *@seelombok.Getter*@seelombok.experimental.FieldDefaults*@seelombok.AllArgsConstructor*@seelombok.ToString*@seelombok.EqualsAndHashCode*@seelombok.Data*/@Target(ElementType.TYPE)@Retention(RetentionPolicy.SOURCE)public @interface Value { /** * If you specify a static constructor name, then the generated constructor will be private, and * instead a static factory method is created that other classes can use to create instances. * We suggest the name: 'of', like so: * * <pre> * public @Value(staticConstructor = 'of') class Point { final int x, y; } * </pre> * * Default: No static constructor, instead the normal constructor is public. * * @return Name of static ’constructor’ method to generate (blank = generate a normal constructor). */ String staticConstructor() default '';}
這個(gè)注解的作用是為一個(gè)不可變的實(shí)體類生成一系列與之匹配的代碼。效果等同于以下注解的組合:@Getter @FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE) @AllArgsConstructor @ToString @EqualsAndHashCode。
這其中有一個(gè)注解比較特殊,@FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE),見名知意,這是一個(gè)為字段設(shè)置默認(rèn)屬性的注解,注解的屬性值中,標(biāo)注了是否設(shè)置實(shí)例字段為final,訪問級(jí)別設(shè)置為private。
/** * Adds modifiers to each field in the type with this annotation. *<p>* Complete documentation is found at<a href='http://www.gepszalag.com/bcjs/<https://projectlombok.org/features/experimental/FieldDefaults>' rel='external nofollow' >the project lombok features page for@FieldDefaults</a>. *<p>* If {@codemakeFinal} is {@codetrue}, then each (instance) field that is not annotated with {@code@NonFinal} will have the {@codefinal} modifier added. *<p>* If {@codelevel} is set, then each (instance) field that is package private (i.e. no access modifier) and does not have the {@code@PackagePrivate} annotation will * have the appropriate access level modifier added. */@Target(ElementType.TYPE)@Retention(RetentionPolicy.SOURCE)public @interface FieldDefaults { AccessLevel level() default AccessLevel.NONE; boolean makeFinal() default false;}
若makeFinal是true,則每個(gè)實(shí)例字段(被@NonFinal注解修飾的除外)都會(huì)被final修飾符修飾。若level屬性有值,那么每個(gè)包私有訪問的(即沒有訪問修飾符修飾)和沒有被@PackagePrivate注解修飾的實(shí)例字段都會(huì)被添加一個(gè)與屬性值對(duì)應(yīng)的修飾符。
也就是說,@Value標(biāo)記了此POJO類為不可能變的類,其所有沒有被@NonFinal注解修飾的成員變量,都會(huì)被final修飾
事情到了這里,還是不知道問題在哪里(Java基礎(chǔ)差)。繼續(xù)找解決辦法。
Google搜索找到此問答:
Lombok @Wither, @Value, @NoArgsConstructor, @AllArgsConstructor do not work together
回答中有一段對(duì)于Java final的描述:
A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a 'blank final' variable. A blank final instance variable of a class must be definitely assigned in every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error occurs in both cases.
翻譯如下:一個(gè)final修飾的變量只能通過初始化器或賦值語(yǔ)句初始化一次。它不需要在聲明處初始化:這被稱為“空白final”變量。類的空白final實(shí)例變量必須在聲明它的類的每個(gè)構(gòu)造函數(shù)中確定賦值;同樣,空白final靜態(tài)變量必須在聲明它的類的靜態(tài)初始化器中明確賦值;否則,以上兩種情況下都會(huì)發(fā)生編譯錯(cuò)誤。
真相大白,原因是在原來的構(gòu)造器中沒有對(duì)新加入的字段進(jìn)行初始化。至此,問題解決。
到此這篇關(guān)于Java中Lombok @Value注解導(dǎo)致的variable not been initialized問題的文章就介紹到這了,更多相關(guān)Java Lombok @Value注解導(dǎo)致問題內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. XHTML 1.0:標(biāo)記新的開端2. 基于javaweb+jsp實(shí)現(xiàn)學(xué)生宿舍管理系統(tǒng)3. 多級(jí)聯(lián)動(dòng)下拉選擇框,動(dòng)態(tài)獲取下一級(jí)4. js判斷兩個(gè)數(shù)組是否存在相同元素的四種方法5. 教你使用idea搭建ssm詳細(xì)教程(Spring+Spring Mvc+Mybatis)6. PHP擴(kuò)展之URL編碼、解碼及解析——URLs7. PHP基礎(chǔ)之?dāng)?shù)據(jù)類型6——對(duì)象(Object)8. 在vue中獲取wangeditor的html和text的操作9. python 實(shí)現(xiàn)的車牌識(shí)別項(xiàng)目10. 最新JavaScript判斷是否是360瀏覽器方法
