Java Local Inner Class in a METHOD, how is it handled by the JVM?
<p>If you look at the method below you will se a declared local class in a method. The reason why, is that the styleIt0 method for example is used several times and only in this method. It is only relevant to this method alone. </p>

<p>Breaking it out to a private method does not make it very clear as to where it is used and what it does just by looking it. I find having large blocks of code unreadable and often would like to break pieces out in small methods, although they are only relevant to one method. </p>

<p>Doing that for several methods in a class will make it very unclear where each private method is intended for and where it is used and that it is only used by a single method, compared to other private methods that might be shared between several methods. </p>

<p>That is why I sometimes prefer to declare other methods in a method. It would be great to be allowed to declare methods in a method in the "standard" fashion but the only way I believe something like that is possible up to Java 6, is by declaring methods in a local innerclass in the method as seen below. </p>

<p><strong>My question here is</strong>: </p>

<p>Is there any performance concerns of such usage? What is the memory footprint for such a declared class and by repeated calls to a method with a local innerclass? Will the JVM compile this class once, at compile time, or is it treated in some other way? </p>

<p><strong>Other thoughts:</strong><br>
Local innerclasses cannot be declared static, and their methods and properties cannot be static. I wonder why!? </p>

<p>Now I am forced to create a new instance each time the method is called. It would have been great to have static methods in a local innerclass and I see no good reason as to why that couldn't be possible? Inner methods would have been even better!</p>

<p><strong>Some</strong> might argue that this is way of coding is not useful, is ugly or that such classes could be declared somewhere else. Please do not hijack the thread in that direction. I happen to find inner methods useful no matter what some might argue. My main interest relates to the performance issues of such usage in the Java language. </p>

<p>Thanks ( code below )</p>

<pre><code>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) {
            // Style the table
            double numberOfCakes = width / PADDING;
            int firstColumnWidth = (int) ((3 / 12d) * numberOfCakes * PADDING); // Take 3/12 of the cakes and calculate the width
            table.setColumnWidth(0, firstColumnWidth);

            table.setColumnWidth(1, width - firstColumnWidth - PADDING * 2); // Minus padding because we use that one for moveTo at the end, and we do want padding
                                                                             // at the end
            Line border = table.createBorder();
            border.setLineStyle(Line.PEN_PS_DASH);
            border.setLineColor(Color.black);
            border.setLineWidth(1.0);
            table.setAllBorders(border);
        }

    }
    Styler styler = new Styler();
    EmployeeCV employeeCV = user.getEmployeeCv();               
    Table table = new Table(3, 2);        
    styler.styleTable(table);        

    // == Cells ==
    // Row 0 = Specific strengths
    TextRun textRun = table.getCell(0, 0).getTextRun();
    RichTextRun rt = textRun.getRichTextRuns()[0];
    rt.setText("Specific strengths: ");
    styler.styleIt0(rt);

    // Content column
    textRun = table.getCell(0, 1).getTextRun();
    rt = textRun.getRichTextRuns()[0];

    StringBuffer s = new StringBuffer();
    List&lt;LeadershipTopStrengths&gt; strengths = employeeCV.getTopStrengthsList();
    int i = 0;
    while (i &lt; strengths.size()) {

        LeadershipTopStrengths strength = strengths.get(i);

        if ( strength != null ) {
            s.append(safeEnumItemDescription(strength));

            // Add newline but not on the last one
            if (i &lt; (strengths.size() - 1) )  { 
                s.append(SIMPLE_NEWLINE);
            }

            rt.setText(s.toString());
            styler.styleIt1(rt);
        }

        i++;
    }        

    // Row 1 = Mobility
    textRun = table.getCell(1, 0).getTextRun();
    rt = textRun.getRichTextRuns()[0];
    rt.setText("Mobility: ");
    styler.styleIt0(rt);

    // Content column
    textRun = table.getCell(1, 1).getTextRun();
    rt = textRun.getRichTextRuns()[0];

    s = new StringBuffer();
    List&lt;InternationalMobility&gt; mobilities = employeeCV.getInternationalMobilityList();
    i = 0;
    while (i &lt; mobilities.size()) {

        InternationalMobility mobility = mobilities.get(i);            
        if(mobility != null){
            s.append(safeEnumItemDescription(mobility));    

            // Add newline but not on the last one
            if (i &lt; (mobilities.size() - 1) )  { 
                s.append(SIMPLE_NEWLINE);
            }

            rt.setText(s.toString());
            styler.styleIt1(rt);

        }
        i++;
    }               

    // Row 2 = Additional
    textRun = table.getCell(2, 0).getTextRun();
    rt = textRun.getRichTextRuns()[0];
    rt.setText("Additional information: ");
    styler.styleIt0(rt);        

    // Content column
    // TableCell cell = table.getCell(2, 1);


    slide.addShape(table);
    table.moveTo(PADDING, height / 2 + PADDING * 2); // MoveTo must come after
}
</code></pre>
