Java Beans

Java bean is the java class that is developed by the following standards:-

· Class must be public.

· Recommended by implementing Serializable

· Bean properties(member Variable) should be private and non-static.

· Every Bean property should have one getter-setter method

· Should have a zero-param constructor directly or indirectly.

3 Class of java beans:-

VO(Value Object Class) — To hold input or outputs

DTO(Data Transfer Object) — To carry data from one layer to another layer or from one project to another project.

BO Class/Entity Class/Model Class — To hold persistable /persistent data

Well Designed java class should contain –

Overloaded Constructor

toString()

equals()

hashcode()

getter-setter methods

Before Lombok API-

We should manually make java classes as well designed by adding above said methods, and we should manually increase or decrease getter-setter methods based on a number of properties. we are adding and removing.

With Lombok API-

All the above things will be taken care of and will be generated internally.

Lombok API also called as project Lombok generates following boilerplate of java class-

a) Constructor

b) Getter-setter

c) toString()

d) equals()

e) hashCode

**It is open source and must be configured with IDE by supplying the following a bunch of annotations:-

@setter , @getter , @ AllArgsConstructor , @NoArgsConstructor , @ToString , @RequiredArgsConstructor , @EqualsAndHashCode , @Data

Note:-

*Lombok API annotations make the java compiler generate certain code dynamically in the .class file.

*Java compiler has the ability to add code dynamically which is not in .class file as a constructor ,getter-setter.

*Lombok annotations Retention level is source i.e those annotations won’t be recorded to .class file, but because of Lombok annotations instructions the code generation by javac compiler like setter-getter, toString() will be recorded into .class file.

https://projectlombok.org/api/
https://projectlombok.org/api/
https://projectlombok.org/api/

@ ToString

Makes the Javadoc to generate/ override toString() having logic to display Object data.This is applicable only at the top of classes.

If don’t override this method is our class then java.lang.Object class toString() executes and this method gives <fully qualified class name>@<hexadecimal notation of hashCode>

Question — what happens if we place toString() explicitly in our class along with Lombok API @ ToString.

Answer — @ ToString of Lombok API won’t give instructions javac to generate toString() because some method is already available to .java file(warning will come fo @ ToString).

Example-

Question — Can two Objects have the same hashCode?

Answer — If hashcode() method java.lang.Object class is called then not possible because it generates hashCode based hashing algorithm as unique number. We override the hashCode() method, our code generates hashCode based on the state of the object. If two Object has the same state then hashCode will be the same.

Question — Can one object has two hashCode?

Answer — If we override the hashcode() method then based on the state of the object one hashcode will be generated, but internally JVM maintains another hashCode based on a hashing algorithm.

Question — What is the use of the .equals() method?

Answer — It is used to compare the state of two objects. By default .equals() method used from the Object class to compare the reference but if we override it then it is used to compare the state.

*In Object class .equals() use to compare references but wrapper classes like String override it and use it to compare content but not reference.

Note- The only compiler that generates a zero-param constructor is called a default constructor, If we place a zero-param constructor explicitly or if Lombok generates the zero-param constructor then that should not be called default constructor.

**Constructor can be overloaded but can’t be overridden.

@ RequireArgsConstructor

Allow generating parameterized constructor involving our choiv=ce number of properties field. The property that we want to involve should be annotated with @ NonNull.

***If no properties annotate with @NonNull it will give a zero-param constructor.

@NoArgsConstructor(access = AccessLevel.PUBLIC)

@AllArgsConstructor(access =AccessLevel.PRIVATE)

@RequiredArgsConstructor(access =AccessLevel.PRIVATE)

@ Data = @ Setter + @ Getter + @ EqualsAndHashCode + @ ToString + @RequiredArgConstructor

Note- @Data and @ AllArgsConstructor

In the use of both only one constructor will get involved that is all param constructor.

Until and Unless @NoArgsConstructor is used 1-param or 0-param constructor will not invoke.

@AllArgsConstructor

@NoArgsConstructor

@ToString

public class Comment {

private Long id;

@NonEmpty

private String text;

Note -

Dependency

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.18.8</version>

<scope>compile</scope>

</dependency>

Software developer