The Java module in Mule 4 intends to:
Minimize the need for custom code.
Avoid the need for you to know or understand Java.
However, advanced some uses cases exist require integrating with custom Java code, for example:
Need to reuse a tax calculation library.
Need to reuse a canonical object model that is standard in the organization.
Need to execute custom logic and Java is your weapon of choice.
Note that in Mule 3, MEL was used as a bridge for creating instances of particular Java classes and executing Java methods. In Mule 4, interoperation with Java changes due to the replacement of MEL by DataWeave, a functional language. Since functional languages should not have side effects on their input arguments, it does not make sense for DataWeave to execute random instance methods on random object instances.
The goals of the Java module can be achieved executing Groovy code through the Scripting module.
However, the Scripting module does not offer DataSense support.