If you look at the method below, you will declare a local class in the method. The reason is that the styleIt0 method, for example, is used several times and only in this method. This applies only to this method.
Breaking it down into a private method makes it impossible to understand where it is used and what it does, just by looking at it. I find that large blocks of code are unreadable and often I want to break up the fragments using small methods, although they only apply to one method.
Doing this for several methods in a class will be very unclear where each private method is intended and where it is used, and that it is used by only one method, compared to other private methods that can be shared between several methods.
This is why I sometimes prefer to declare other methods in a method. It would be great if it were allowed to declare methods in a method in a “standard” way, but the only way I think this is possible before Java 6 is to declare methods in a local inner class in the method, as shown below.
My question is here :
Are there any performance issues with this use? What is the amount of memory for such a declared class and repeated calls to a method with a local inner class? Will the JVM compile this class once, at compile time, or be handled in some other way?
Other thoughts:
Local inner classes cannot be declared static, and their methods and properties cannot be static. I wonder why!?
Now I am forced to create a new instance each time the method is called. It would be great to have static methods in a local inner class, and I see no good reason why this is not possible? Internal methods would be even better!
Some may argue that this coding method is not useful, ugly, or that such classes can be declared elsewhere. Please do not grab a thread in this direction. I happen to find internal methods useful no matter what some might argue. My main interest is related to performance issues of such use in the Java language.
Thanks (code below)
private void addBottomTable(Slide slide, User user) { class Styler { public void styleIt0(RichTextRun rt) { rt.setFontName(font); rt.setFontSize(12); rt.setBold(true); } public void styleIt1(RichTextRun rt) { rt.setFontName(font); rt.setFontSize(10); rt.setBold(true); } public void styleTable(Table table) {