It's important to know what version you're using. In the last versions, Java customizations are less recommended, because the tend to be troublesome when upgrading. But they still work well.
If you really want to do it in Java, I recommend that you implement a ModelListener and deploy it to Liferay.
https://help.liferay.com/hc/en-us/articles/360018170891-Model-Listener
Hope this helps