1

I am creating a simple text editor program with Sublime Text on Mac and when I am compiling via the command line it produces 6 .class files.

I am using the following command in the terminal to compile my class.

javac Notepad.java

The console log is shown in the image below...

Console Log

Does anyone know why it could be producing multiple .class files? And the weird thing is I can delete them and then use java Notepad and it still runs perfectly... Why does Java even create these? I'm sure I am doing something stupid.

Rather than post the full code, here is my git repository.

Dummy Code
  • 1,858
  • 4
  • 19
  • 38

5 Answers5

9

Those are names given to inner classes -- it does not mean you are doing anything wrong, or that there's anything wrong with the code.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Ah... But the weird thing is I can delete them and then use `java Notepad` and it still runs perfectly... Why does Java even create these? – Dummy Code Jul 19 '13 at 15:01
  • 3
    @DaveNewton -1 taken back, +1 given. I'm kind of out of it today. Apologies to author. – nanofarad Jul 19 '13 at 15:03
  • 3
    @HenryHarris: that just means that your test hasn't executed the lines of code where these classes are needed. The class files are not generated for nothing. They are needed. – JB Nizet Jul 19 '13 at 15:05
  • @JBNizet Ah... So it will run, it will just break when I try and execute a method with these lines of code in them? – Dummy Code Jul 19 '13 at 15:07
  • @Henry Harris - my first guess is that, although you have inner classes created in your code, the "perfect" run doesn't use them. But one would have to inspect the code and look at the classes to know. Java creates these because they are necessary to implement the instructions represented by the source code; they weren't put there to confuse you or annoy us. – arcy Jul 19 '13 at 15:08
  • 1
    @HenryHarris: You got it. If you edited your question and showed your code, we could tell you whichlines in the code cause these class files to be generated. – JB Nizet Jul 19 '13 at 15:09
  • @rcook Got it. Maybe I can clean it up or call a new method to create a new "notepad" and then it will stop. Is that a _better_ way to do it? Or is this code perfectly fine? – Dummy Code Jul 19 '13 at 15:09
  • @JBNizet I added a comment containing my github. – Dummy Code Jul 19 '13 at 15:10
  • @HenryHarris: each `new ActionListener() { ... }` you define is actually an anonymous inner class, compiled to `Notepad$1.class`, then `Notepad$2.class`, etc. Having them generated is completely normal. – JB Nizet Jul 19 '13 at 15:14
  • I haven't read your code, so I don't know if it's "perfectly fine". You can eliminate the 'extra' class files by not using inner classes, though if, as other comments indicate, you are using Swing or another event-driven framework, you may need extra classes somewhere else if you do that. – arcy Jul 19 '13 at 15:37
1

In your code (on line 74) there is:

new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        new Notepad(true);
    }
}

this is an anonymous class, so it will appear in another .class file. I think you have several which is why you have several files. You also have several other of these for save etc.

Following your comment to @rcook I suspect that not all the functions are working. You can probably open but you may not be able to save etc

This previous question explains the name format

Community
  • 1
  • 1
RNJ
  • 15,272
  • 18
  • 86
  • 131
1

Each anonymous class (as per an interface in-line or @Override with a class) is compiled to its own .class file based on the definition you give and the interface/class itself.

When the block of code using that anonymous class is reached one or more times the class that was compiled gets instantiated.

The line instantiating it gets compiled down to a simple instantiation, and the class is reused for the same line.

If the inner class .class files are deleted you'll get exceptions and errors on the lines where they are used. If those lines aren't reached then you'll never get errors or exceptions as the check occurs at runtime.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

anonymous class creates its own class file. See here for similar question Why does Java code with an inner class generates a third SomeClass$1.class file?. Basically separate class is created for each inner class. Anonymous class is a type of inner class.

for inner classes in java look at here http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314
0

You probably are creating some inner classes, when you compile your classes, the inner class files will be compiled as $InnerFile.class