Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why does javac create 2 class files

 
Allen Bandela
Ranch Hand
Posts: 128
Eclipse IDE MS IE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Allen Bandela
Ranch Hand
Posts: 128
Eclipse IDE MS IE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic