In conclusion, the interface does not automatically generate Getters and Setters. If you understand the nature of the interface, it's obvious, but I'm personally confused, so I'll summarize it.
The development environment is as follows.
I think you often create classes in Java coding. I think there are many people who define the following getters and setters every time. (At least I was)
//Ordinary class
public class SampleClass {
//Member variables
private String foo;
// Getter
public String getFoo() {
return this.foo;
}
// Setter
public void setFoo(String foo) {
this.foo = foo;
}
}
//Caller
public void SampleMethod {
//Ordinary class
SampleClass sample1 = new SampleClass();
sample1.setFoo("hoge");
System.out.println("sample1.getFoo(): " + sample1.getFoo());
}
For example, if you have a lot of classes and member variables (fields) and you have to code a lot of simple methods like getters like this, many people will copy and paste.
Even if you make a mistake, it's hard to notice this kind of copy and paste, so I think it's easy to become a hotbed of bugs.
Lombok solves that problem. Just write the following and it will automatically generate getters and setters. (Something like C # properties)
import lombok.Data;
//In class@Add Data
@Data
public class SampleClassWithLombok {
//Member variables
private String foo;
}
//Caller
public void SampleMethod {
// @Class with Data
SampleClassWithLombok sample2 = new SampleClassWithLombok();
sample2.setFoo("hoge");
System.out.println("sample2.getFoo(): " + sample2.getFoo());
}
Now I've reduced the amount of code and crushed the hotbed of bugs.
Please refer to the introduction method of Lombok, which was summarized here before.
-Introduce Spring Tool Suite to Windows -Install Spring Tool Suite on Mac
I was wondering what happens if I add @Data
to the interface. about it.
Since private
cannot be specified for the member variable of the interface, I would like to add getter and setter to the member variable that is public
. (It doesn't make sense)
Let's add @Data to an interface like this.
//On the interface@Add Data
@Data
public interface SampleInterface {
//Member variables
public String bar = "bar";
}
As soon as I turned it on, Eclipse threw the error @Data is only supported on a class.
.
Since the member variables of the interface are constants in the first place, I think the reason is that we cannot define a setter.
What about the implementation class? Suppose you have an interface and implementation class like this:
//interface
public interface SampleInterface {
}
import lombok.Data;
//In implementation class@Add Data
@Data
public class SampleImpl implements SampleInterface {
//Member variables
private String foo;
}
//Caller
public void SampleMethod {
// @Implementation Class with Data
SampleInterface sample3 = new SampleImpl();
sample3.setFoo("hoge");
System.out.println("sample3.getFoo(): " + sample3.getFoo());
}
When I wrote code like this, I didn't get an error in the interface / implementation class, but I got an error in the caller that method setFoo (String) is undefined in type SampleInterface
.
It seems that getter and setter are defined in the implementation class, but it seems that an error occurred because the interface does not have that definition.
Here, I added getter and setter to the interface.
//Getter on interface,Define setter
public interface SampleInterface {
// Getter
public String getFoo();
// Setter
public void setFoo(String foo);
}
Now the error is gone. However, I ended up defining getters and setters for the interface. With this, you can't suppress copy-pemis.
What about abstract classes? Suppose you have an abstract class and an inheritance class like this.
import lombok.Data;
//In abstract class@Add Data
@Data
public abstract class SampleAbstract {
//Member variables
private String foo;
}
//Inheritance Class
public class SampleExtends extends SampleAbstract {
}
//Caller
public void SampleMethod {
// @Abstract Class with Data
SampleAbstract sample4 = new SampleExtends();
sample4.setFoo("hoge");
System.out.println("sample4.getFoo(): " + sample4.getFoo());
}
When I wrote a code like this, no error occurred. Unlike the interface, abstract classes can have private
member variables, so getters and setters may also be auto-defined.
For ordinary classes, implementation classes, and abstract classes, the getter and setter of member variables can be automatically defined by adding @Data
.
However, I found that the interface had to manually define getters and setters.
I think that you can easily avoid it by switching to the abstract class, but when you need getters and setters for the interface, it may be better to review the design of the interface once.
Recommended Posts