DEV Community

Cover image for Generating data classes in Java

Generating data classes in Java

Bertil Muth on November 08, 2018

Kotlin has a concise syntax to declare data classes: data class User(val name: String, val age: Int) Enter fullscreen mode Exi...
Collapse
 
petarov profile image
Petar G. Petrov

Pretty nice! This could be a good approach when designing large systems with lots of models.I actually had a similar idea in the past, because I needed to generate getters/setters for about 100+ POJOs.

Right now I'm just mixing Kotlin and Java, so I benefit from Kotlin's Data classes whilst using Java for the most parts.

Collapse
 
bertilmuth profile image
Bertil Muth

Yes, there are many ways to reach a goal!:-) As you said, my approach is especially useful with big numbers of classes, and if you have to customize the generated classes further.

Collapse
 
fnh profile image
Fabian Holzer • Edited

What will you do?

I will continue to let IntelliJ do it for me...

Collapse
 
bertilmuth profile image
Bertil Muth

Haha, sure.

Collapse
 
smuschel profile image
smuschel

I got some useful ideas from your post. Thank you for that. IT seems that Java 12 introduces a record concept. See

Collapse
 
bertilmuth profile image
Bertil Muth

I am glad you found it interesting - I recently read the article you referenced, I didn’t know about Java 12‘s record before. It looks like a spitting image of Kotlin.

Code generation is useful beyond it, of course, it was just a simple example.

Collapse
 
lemuelogbunude profile image
Lemuel Ogbunude • Edited

We weren't promised that Java 12 would have Records, it's something they are looking at though, if not in Java 12 then in Java 13+. For now, we could use your solution :).

Thread Thread
 
bertilmuth profile image
Bertil Muth

Good to know, thanks.

Collapse
 
helpermethod profile image
Oliver Weiler