This example defines a PersonDTO with a nested ContactDTO inside it. The main method creates a person with contact details and prints them.
package com.example.demo.dto;
public class PersonDTO {
private String name;
private ContactDTO contact;
public PersonDTO() {}
public PersonDTO(String name, ContactDTO contact) {
this.name = name;
this.contact = contact;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ContactDTO getContact() {
return contact;
}
public void setContact(ContactDTO contact) {
this.contact = contact;
}
public static class ContactDTO {
private String email;
private String phone;
public ContactDTO() {}
public ContactDTO(String email, String phone) {
this.email = email;
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
}
// Usage example in a main method or test
class Main {
public static void main(String[] args) {
PersonDTO.ContactDTO contact = new PersonDTO.ContactDTO("alice@example.com", "123-456-7890");
PersonDTO person = new PersonDTO("Alice", contact);
System.out.println("Name: " + person.getName());
System.out.println("Email: " + person.getContact().getEmail());
System.out.println("Phone: " + person.getContact().getPhone());
}
}