Why does the compiler create repeated . class files?

Asked

Viewed 264 times

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 netbeans or if it really was the compiler and when 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 compiling:

print dos arquivos .class

Until I understand he created the Tela$DialogTela.class, I believe because it is an inner class, but I don’t understand why the compiler created the Tela$1.classand the Tela$2.class, beyond the very Tela.class.

For consciousness-raising, I created another class called Teste, where I just make a call to a JOptionPane(which is also from the swing package):

import javax.swing.JOptionPane;

public class Teste{

    public static void main(String[] args) {
    JOptionPane.showMessageDialog(null, "teste", "Teste", JOptionPane.INFORMATION_MESSAGE);
    }
}

and was generated only the Teste.class.

What are those repeat classes that the compiler created from the same class for? It’s some particularity of the swing API?

1 answer

10


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

In the example of the question:

  • Screen.class ---> Java screen.
  • Screen$Dialogtela.class ---> Inner class (Inner class) called Dialogtela
  • Screen$1.class ---> Internal anonymous class (new Runnable() {...})
  • Screen$2.class ---> Internal anonymous class (new ActionListener() {...})

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

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

  • So the answer to the question was already in it :D I was so disappointed with myself when I read your answer that I did a test based on the answer, using Thread, without swing and without anything, and the result was exactly this.

  • 1

    @diegofm, at the time you were commenting I edited my answer exactly saying that this is Java in general. Pure coincidence. : -) Do not be disappointed, your question is very well written and will serve many people.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.