r/learnjava • • Feb 05 '26

is this a valid way

So I'm curious about how to use record, because record are immutable and suitable for DTO. I tried to combine it with class, like this

public class UserDto {
    public record ReqRegister() {
    }
    public record ReqLogin() {
    }
    public record ResUser() {
    }
    public record ResLogin() {
    }
}

I think this is readable and easy to maintain, and reduces the number of files, so what do you suggest?

1 Upvotes

13 comments sorted by

View all comments

Show parent comment

1

u/notBot000_ Feb 06 '26

That makes sense. I realize now I was mostly following an existing layering habit (DTO package, etc.) rather than thinking in terms of what actually evolves together. That’s a useful insight, thanks.