Why does the compiler create repeated .class files?

9

I have noticed that when the java compiler compiles some class that has some swing / awt component, it creates another or even several classes of the same name, with a $ followed by a numbering.

I made a simple code to check if it was a netbeans thing or if it was really the compiler and compiling the code below:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Tela {

    public Tela(){

        final JFrame frame = new JFrame("Tela 1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel label = new JLabel("teste");
        JButton button = new JButton("botao");
        button.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){

            new DialogTela(frame, true);
            }

        });
        frame.setPreferredSize(new Dimension(175, 100));
        JPanel p = new JPanel(new FlowLayout());
        p.add(label);
        p.add(button);
        frame.getContentPane().add(p);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Tela();
            }
        });
    }

    class DialogTela{

        public DialogTela(Frame f, boolean modal){

        JDialog dialog = new JDialog(f,modal);

            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

            JLabel label = new JLabel("teste");
            dialog.setPreferredSize(new Dimension(175, 100));
            JPanel p = new JPanel(new FlowLayout());
            p.add(label);
            dialog.getContentPane().add(p);

            dialog.pack();
            dialog.setVisible(true);
        }
    }   
}

Result after compile:

I'mnotsurewhythecompilercreatedtheTela$DialogTela.classandtheTela$1.class,inadditiontotheTela$2.classitself.

Forthesakeofconscience,IcreatedanotherclasscalledTela.class,whereIonlymakeacalltoaTeste(whichalsobelongstotheswingpackage):

importjavax.swing.JOptionPane;publicclassTeste{publicstaticvoidmain(String[]args){JOptionPane.showMessageDialog(null,"teste", "Teste", JOptionPane.INFORMATION_MESSAGE);
    }
}

and only JOptionPane was generated.

What would be those repeated classes that the compiler created from the same class? Is there any particularity of the swing API?

    
asked by anonymous 28.06.2016 / 20:15

1 answer

10

A .java file can generate multiple .class files. The Java compiler (javac) generates more than one .class when there is an internal class and / or internal anonymous class, for which the anonymous classes are numbered, this is the reason for Screen $ 1.class and Screen $ 2.class.

In the question example:

  • Tela.class --- > Screen.java
  • Screen $ DialogTable.class --- > Internal class (inner class), called DialogTel
  • $ 1.class Screen --- > Internal anonymous class (% with%)
  • $ 2.class Screen --- > Internal anonymous class (% with%)

In the case of internal anonymous classes, I would not know exactly what the Screen $ 1.class or Screen $ 2.class is. You need to know the order in which things are compiled by Javac. The example above, therefore, was arbitrary, by way of example.

A detail: This Java compiler engine is applied to any type of application that is compiled by it (Swing, J2EE, Command Line, etc.).

    
28.06.2016 / 20:34