Java中Lombok @Value注解導(dǎo)致的variable not been initialized問(wèn)題
想要修改一個(gè)POJO類(lèi),在其中增加一個(gè)字段,但是增加以后就開(kāi)始報(bào)錯(cuò):
這個(gè)注解的作用是為一個(gè)不可變的實(shí)體類(lèi)生成一系列與之匹配的代碼。效果等同于以下注解的組合:@Getter @FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE) @AllArgsConstructor @ToString @EqualsAndHashCode。
這其中有一個(gè)注解比較特殊,@FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE),見(jiàn)名知意,這是一個(gè)為字段設(shè)置默認(rèn)屬性的注解,注解的屬性值中,標(biāo)注了是否設(shè)置實(shí)例字段為final,訪問(wèn)級(jí)別設(shè)置為private。
/** * Adds modifiers to each field in the type with this annotation. *<p>* Complete documentation is found at<a href='http://m.baoyu77737.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è)包私有訪問(wèn)的(即沒(méi)有訪問(wèn)修飾符修飾)和沒(méi)有被@PackagePrivate注解修飾的實(shí)例字段都會(huì)被添加一個(gè)與屬性值對(duì)應(yīng)的修飾符。
也就是說(shuō),@Value標(biāo)記了此POJO類(lèi)為不可能變的類(lèi),其所有沒(méi)有被@NonFinal注解修飾的成員變量,都會(huì)被final修飾
事情到了這里,還是不知道問(wèn)題在哪里(Java基礎(chǔ)差)。繼續(xù)找解決辦法。
Google搜索找到此問(wèn)答:
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修飾的變量只能通過(guò)初始化器或賦值語(yǔ)句初始化一次。它不需要在聲明處初始化:這被稱(chēng)為“空白final”變量。類(lèi)的空白final實(shí)例變量必須在聲明它的類(lèi)的每個(gè)構(gòu)造函數(shù)中確定賦值;同樣,空白final靜態(tài)變量必須在聲明它的類(lèi)的靜態(tài)初始化器中明確賦值;否則,以上兩種情況下都會(huì)發(fā)生編譯錯(cuò)誤。
真相大白,原因是在原來(lái)的構(gòu)造器中沒(méi)有對(duì)新加入的字段進(jìn)行初始化。至此,問(wèn)題解決。
到此這篇關(guān)于Java中Lombok @Value注解導(dǎo)致的variable not been initialized問(wèn)題的文章就介紹到這了,更多相關(guān)Java Lombok @Value注解導(dǎo)致問(wèn)題內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類(lèi)別頻數(shù)數(shù)據(jù)畫(huà)水平條形圖案例2. python中PyQuery庫(kù)用法分享3. python操作數(shù)據(jù)庫(kù)獲取結(jié)果之fetchone和fetchall的區(qū)別說(shuō)明4. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫(huà)特效5. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總6. php5.6不能擴(kuò)展redis.so的解決方法7. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)8. Ajax實(shí)現(xiàn)頁(yè)面無(wú)刷新留言效果9. AJAX實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺(tái)】10. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值
