简体   繁体   中英

Setter annotation is not working in Java class

My workspace structure is like:

 package1
     Class A
 package 2
    Class B

The class A has all the annotations added to it:

@Getter
@Setter
@Value
@Data
@ToString
@Builder
class A {
    int a,
    int b
}

But when I try to use A.setA(1) in class BI get error that setA is not a function defined.

I have included the following dependency in config:

LombokUtils = 1.1;
Lombok = 1.16.x; 

Not sure what is getting wrong here. @Setter annotation is not working.

@Getter

This tells lombok to make getters. But, @Data also does this, and @Value also does this. There is no point to this annotation; remove it.

@Setter

This tells lombok to make setters for all non-final fields (spoiler: Your fields are final here, so no setters are made). But, @Data also does this - it also means: Make a setter for every non-final field. There is no point to this annotation; remove it.

@Value

This tells lombok to make the class final, to make all fields final , to make all fields private, make a constructor that sets all fields, make a toString, make getters, and make hashcode and equals methods.

Because it makes all fields final, this makes @Setter do nothing.

@Data

You should not annotate a class with both Data and Value: They are opposites. If you intend for those fields to be settable, you're looking for @Data , and not for @Value . Delete one of them.

@ToString

Both Data and Value already do this; There is no point to this annotation; remove it.

@Builder

That's not implied by anything else, so keep it.

I'm pretty sure you want:

@Data @Builder class Foo {
}

and nothing else. Actually, you have builder, so you may want an immutable class (so, no setters at all - you construct an instance and it is then not changable), in which case, you're looking for @Value @Builder class Foo {} .

There's a conflict between @Value (which helps creating immutable classes) and @Setter (which adds methods that mutates the class state).

Remove the @Value annotation, and you should have setters in class A.

Based on lombok's reference here :

@Value is the immutable variant of @Data; all fields are made private and final by default, and setters are not generated.

@Value creates immutable classes while @Setter makes classes mutable by adding setters.

So in order to have setters remove @Value .

check this out without @Value:

package com.samples.demo.pacakge1;

import lombok.*;

@Data
@Getter
@Setter
@Builder
//@Value
@ToString
public class A {
    int x;
    int y;
}

and class B:

package com.samples.demo.package2;

import com.samples.demo.pacakge1.A;

public class B {
    public static void main(String[] args) {
        A a = A.builder().build(); //can't use A a=new A();
        a.setY(12);
        System.out.println(a.getY());

    }
}

Is not working becasue in the class A you need to import lombok.Getter; and import lombok.Setter; Now in your B class you should be able to access the setter. You can get more information Here

change the visibility of the class A as public and try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM