This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes why does javac create 2 class files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why does javac create 2 class files" Watch "why does javac create 2 class files" New topic
Author

why does javac create 2 class files

Allen Bandela
Ranch Hand

Joined: Feb 16, 2006
Posts: 128

When i execute my applet using javac or eclipse, two class files are created for the same file.
one is named filename.class and the other filename$1.class
what is the second file for? It has a size of 1kb while the first has a larger size of around 60kb.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

It's an anonymous inner class. Every inner class, named or not, gets its own class file. Anonymous ones are numbered, as this one is, as they're encountered in the file, top to bottom.


[Jess in Action][AskingGoodQuestions]
Allen Bandela
Ranch Hand

Joined: Feb 16, 2006
Posts: 128

Thanks for the answer. Is there any way , I can detect any remove those anonymoys class files? I'm not able to upload the class file with the dollar sign to my website. So, I want to get rid of it.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

What a great example this is of why we want you to Carefully Choose One Forum for each post here at the Ranch: we're having the same conversation twice, in two forums. The other half is here. Since that one is off-topic for the forum it's in, I'm going to close it, and leave this one open for followup.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It really woudn't be desireable to remove this class. That would most likely cause the program to throw an error when it tries to look up a class definition that's not there. The only reason this might not happen is if you don't happen to utilize whatever functionality requires the anonymous class. You could try it and see. But most likely, you simply can't remove these anonymous class files.

Your main options are:

1. Solve the upload problem so that you can upload the $ file.
2. Put all the classes you need within a JAR file, and just upload the one JAR file instead.
3. Rewrite the Java code to eliminate any anonymous classes or other nested classes.

I think #2 is probably best in general.


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why does javac create 2 class files
 
Similar Threads
extending class: cannot find symbol ?
debugging with ANT
different size of jars created by ANT and batch command
Use of Javap
Cannot Compile Servlet with Tomcat