Sometimes .java compiles to two class files

M

Mythic Wave

Hello,

Sometimes when I compile a long .java file, I get two class files as a
result. For example, the file servlet.java compiles to servlet.class and
servlet$1.class. Does anyone know why the servlet$1.class file is created
and how to control it?

Thanks,

Brian

(e-mail address removed)
 
N

Nicholas Clarke

Sometimes when I compile a long .java file, I get two class files as a
result. For example, the file servlet.java compiles to servlet.class and
servlet$1.class. Does anyone know why the servlet$1.class file is created
and how to control it?

$1,2... are anonymous inner classes. I doubt you can control it besides
not using inner classes.

-Nicholas
 
D

Daniel Cer

Hello,
Sometimes when I compile a long .java file, I get two class files as a
result. For example, the file servlet.java compiles to servlet.class and
servlet$1.class. Does anyone know why the servlet$1.class file is created
and how to control it?

Thanks,

Brian

It looks like you have an anonymous inner class somewhere in the source file.

Grep for something like

Object someRandomName = new Object() { ...

The 'servlet$1.class' file is just the byte-code for the compiled inner
class.

-Dan
 
J

Juha Laiho

Mythic Wave said:
Sometimes when I compile a long .java file, I get two class files as a
result. For example, the file servlet.java compiles to servlet.class and
servlet$1.class. Does anyone know why the servlet$1.class file is created
and how to control it?

It's not the length. It's that you have an anonymous inner class in
your code. If a single source file contains more than one anonymous
inner classes, you'll get Foo$2.class, Foo$3.class, ... .

See:
http://mindprod.com/jgloss/innerclasses.html
http://mindprod.com/jgloss/anonymousclasses.html
 
R

Roedy Green

Sometimes when I compile a long .java file, I get two class files as a
result. For example, the file servlet.java compiles to servlet.class and
servlet$1.class. Does anyone know why the servlet$1.class file is created
and how to control it?

it is typically an anonymous inner class. If you don't like them,
name your classes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top