Path: csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Java record Date: Wed, 1 Mar 2023 20:23:03 -0500 Organization: A noiseless patient Spider Lines: 41 Message-ID: References: <906f5a76-929d-46cb-b5e0-fcc8e4ad4e0en@googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 2 Mar 2023 01:23:09 -0000 (UTC) Injection-Info: reader01.eternal-september.org; posting-host="4d1fb28c87946106fd48c03755d64fa6"; logging-data="118712"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Q+4k9yMmQ/Dp4m6l2HWpJ/CM57xkE2Ys=" User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Cancel-Lock: sha1:A9V2lzUa1cbWr4b4Tc2vZR6iPqw= Content-Language: en-US In-Reply-To: <906f5a76-929d-46cb-b5e0-fcc8e4ad4e0en@googlegroups.com> Xref: csiph.com comp.lang.java.programmer:39924 On 3/1/2023 2:18 PM, e.d.pro...@gmail.com wrote: > I just saw they added a new thing called record to Java 16. Is this not a replacement for a DTO or am I missing something? > I create a DTO class like: > public class TestDTO { > private String testField; > public String getTestField() { > return this.testField; > } > public void setTestField(String test) { > this.testField = test; > } > } > > I define a matching record definition like: > public record TestRecordDTO(String testField) { > > } > In web, we use DTOs on the back end to pass data to and from the > Javascript/Typescript on the front end. It looks like passing to the > front end works, now how do we use it as a model to get the user > input? Records are just a way to create data classes with less typing. You can use them just like the equivalent manually created data classes. But there are two caveats: 1) they are immutable (only getter, no setter) 2) the getter does not follow Java bean convention - it is .xxxx() not .getXxxx() That may work or may not work with what you need from your model classes. Arne