Inlining in Java

A couple of the other answers have suggested that only final methods can be inlined – this is not true, as HotSpot is smart enough to be able to inline non-final methods so long as they haven’t been overridden yet. When a class is loaded which overrides the method, it can undo its optimisation. Obviously making the method final mean that’s never required…

Basically let the JVM do its job – it’s likely to be a lot better at working out where to inline than you are.

Do you have a situation where you’re convinced that the JVM isn’t doing a good job? Assuming you’re using HotSpot, have you tried using the server version instead of client? That can make a huge difference.

Leave a Comment