Sometimes .java compiles to two class files

Discussion in 'Java' started by Mythic Wave, Aug 18, 2005.

  1. Mythic Wave

    Mythic Wave Guest

    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

    Mythic Wave, Aug 18, 2005
    #1
    1. Advertising

  2. On Thu, 18 Aug 2005, Mythic Wave wrote:

    > 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
    Nicholas Clarke, Aug 18, 2005
    #2
    1. Advertising

  3. Mythic Wave

    Daniel Cer Guest

    > 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
    Daniel Cer, Aug 18, 2005
    #3
  4. Mythic Wave

    Juha Laiho Guest

    "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
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho, Aug 18, 2005
    #4
  5. Mythic Wave

    Mythic Wave Guest

    Yes, an inner class was there. Thanks.

    Brian

    "Mythic Wave" <> wrote in message
    news:...
    > 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
    >
    >
    >
    >
    Mythic Wave, Aug 18, 2005
    #5
  6. Mythic Wave

    Roedy Green Guest

    On Thu, 18 Aug 2005 09:50:35 -0700, "Mythic Wave"
    <> wrote or quoted :

    >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.
    Roedy Green, Aug 18, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders
    Replies:
    0
    Views:
    467
    John Saunders
    Aug 28, 2003
  2. Nehmo Sergheyev
    Replies:
    1
    Views:
    504
    Andrew Urquhart
    May 9, 2004
  3. Marcin Vorbrodt

    ::std sometimes needed, sometimes not

    Marcin Vorbrodt, Sep 16, 2003, in forum: C++
    Replies:
    24
    Views:
    762
    Jerry Coffin
    Sep 17, 2003
  4. Replies:
    1
    Views:
    502
    gkelly
    Nov 29, 2006
  5. Randy Smith
    Replies:
    2
    Views:
    445
    Randy Smith
    Apr 24, 2007
Loading...

Share This Page