Is it possible to create get and set for private fields in a Java class using any library?

Obviously not defining them in code, for example using annotations?

Can Spring (boot) have this feature?

public static class SomeClass { private Integer property1; private String property2; private String propertyN; } 
  • 6
    here, rather, something like lombok will do - Grundy
  • write immediately to Kotlin then =), the generation of the source is a deal with the devil, you need to understand what you are signing =) - Stranger in the Q
  • @StrangerintheQ, cool :-) source code that generates source code! - Grundy
  • @Grundy is cool, only the build engineers swear later - Stranger in the Q
  • one
    @StrangerintheQ, build engineers must suffer :-D - Grundy 2:17 pm

2 answers 2

Judging by the video on the main page , in this case, fit lombok, and annotations @Getter / @Setter

Example from help

Code using annotations:

 import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; public class GetterSetterExample { /** * Age of the person. Water is wet. * * @param age New value for this person's age. Sky is blue. * @return The current value of this person's age. Circles are round. */ @Getter @Setter private int age = 10; /** * Name of the person. * -- SETTER -- * Changes the name of this person. * * @param name The new value. */ @Setter(AccessLevel.PROTECTED) private String name; @Override public String toString() { return String.format("%s (age: %d)", name, age); } } 

Java related code

 public class GetterSetterExample { /** * Age of the person. Water is wet. */ private int age = 10; /** * Name of the person. */ private String name; @Override public String toString() { return String.format("%s (age: %d)", name, age); } /** * Age of the person. Water is wet. * * @return The current value of this person's age. Circles are round. */ public int getAge() { return age; } /** * Age of the person. Water is wet. * * @param age New value for this person's age. Sky is blue. */ public void setAge(int age) { this.age = age; } /** * Changes the name of this person. * * @param name The new value. */ protected void setName(String name) { this.name = name; } } 

    I'm studying right now. Lombok will do just fine, but everything is much simpler - sample code:

     import lombok.Data; @Data public class Taco { private String name; private List<String> ingredients; } 

    The @Data annotation creates everything you need.

    • one
      The @Data annotation creates everything you need. and maybe a little more :) - Grundy
    • As well as the abstract "@Getter" "@Setter" projectlombok.org/features/GetterSetter , if you do not need all the nonsense generated by "@Data" - ActivX