Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Extra Class File  RSS feed

 
Kevin Simonson
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for the heck of it, I wrote two Java files, 766 lines in "ScProgCalc.java", and 361 lines in "BdMath.java"; the former uses the latter. When I compiled the two of them together with the Java compiler I downloaded from the Oracle site (version 1.8.0_40), the compiler generates the following ".class" files:

That looks really messy, so I created a "New" directory, copied "ScProgCalc.java" and "BdMath.java" down there, and extracted files "ExponentPower.java", "Mode.java", and "Status.java" from the original two files. Then when I compiled them again there was just one "BdMath.class" file; no "BdMath$ExponentPower.class" file was generated, because that class wasn't in "BdMath.java" any more. Similarly for "ScProgCalc$Mode.class" and "ScProgCalc$Status.class"; they were both replaced by "Mode.class" and "Status.class". But the "ScProgCalc$1.class" file was still there.

I thought that might be because "ScProgCalc.java" was still pretty big, so I extracted some of the functionality from it and created "Help.java" and "InOut.java". Then I took a look and got:

As you can see, the "ProgCalc$1.class" file is still there. (I renamed "ScProgCalc.java" to "ProgCalc.java" to distinguish it from its monolith ancestor.) Does anybody know why the compiler would take an "Abc.java" file and generate both an "Abc.class" file and an "Abc$1.class" file, and what I can do to stop it from happening? What exactly does the "Abc$1.class" do, anyhow?
 
Henry Wong
author
Sheriff
Posts: 23023
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Simonson wrote:What exactly does the "Abc$1.class" do, anyhow?


The BdMath$1.class file is the class file for the anonymous inner class of the BdMath class. And the BdMath$ExponentPower.class file is the class file for the ExponentPower inner class (or nested class) of the BdMath class.

Henry
 
Dave Tolls
Ranch Hand
Posts: 2551
27
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Henry says, somewhere in your ProgCalc.java you are using an anonymous inner class.
The compiler generates a class file for that of the format <classname>$<incrementing number>.class.

You'll see it a lot in Swing, with anonymous listeners on things like buttons.
 
Campbell Ritchie
Sheriff
Posts: 54012
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
javap -c ProgCalc$1
and see what it says about the class. It will give enough information to work out its declaration.
 
Kevin Simonson
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Try
javap -c ProgCalc$1
and see what it says about the class. It will give enough information to work out its declaration.

Okay, I tried that and I got:

What does that tell me? I couldn't find any place in "ProgCalc.java" that refers to $SwitchMap$Status.
 
Kevin Simonson
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Try
javap -c ProgCalc$1
and see what it says about the class. It will give enough information to work out its declaration.

Oops! Forgot the "-c". I tried it again and got:

I'm still not exactly sure what it's telling me.
 
Henry Wong
author
Sheriff
Posts: 23023
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Simonson wrote:
What does that tell me? I couldn't find any place in "ProgCalc.java" that refers to $SwitchMap$Status.


Do you really actually need to take this route to find the anonymous inner class? How much code (in the ProgCalc class) are we talking about here?

What is wrong with just doing a find on the "new" keyword, and find the one that is instantiating an anonymous inner class?

Henry
 
Kevin Simonson
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Kevin Simonson wrote:
What does that tell me? I couldn't find any place in "ProgCalc.java" that refers to $SwitchMap$Status.


Do you really actually need to take this route to find the anonymous inner class? How much code (in the ProgCalc class) are we talking about here?

What is wrong with just doing a find on the "new" keyword, and find the one that is instantiating an anonymous inner class?

Henry

My computer is a Windows 7 machine, so I don't have the Unix grep command I'm used to, so I wrote a Java program that does the equivalent for me. So when I search on the "new" keyword I get:

Do any of these look like they are instantiating an anonymous inner class?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12463
43
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<aside>I believe most windows version have a command line untility named "findstr" that does much of what grep does...</aside>
 
Campbell Ritchie
Sheriff
Posts: 54012
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the SwitchMap class? Does it have a field called Status?
Your output from javap is slightly different from what I am used to; it doesn't say anything about implements or extends.
 
Dave Tolls
Ranch Hand
Posts: 2551
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a lambda expression anywhere in your code?
 
Campbell Ritchie
Sheriff
Posts: 54012
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A λ won't produce an anonymous class. The anonymous class I wrote for this thread had something like implements java.lang.Runnable appearing in its printout via javap.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!