$ in Stacktrace

C

Christoph

Hello,

i have a simple question. In my stacktrace i often find the signs "$1" or
"$2" behind a classname, for example:

"java.lang.ClassCastException: de.gebit.geplan.AllTests$1"

What does it mean? I couldn´t find any documentation on it.

Thanks for your help.
Christoph
 
R

Robert Klemme

Hello,

i have a simple question. In my stacktrace i often find the signs "$1" or
"$2" behind a classname, for example:

"java.lang.ClassCastException: de.gebit.geplan.AllTests$1"

What does it mean? I couldn´t find any documentation on it.

These are classes that are not explicitly declared like anonymous inner
classes. When you look into your classpath you'll see them.

Kind regards

robert
 
A

Andrew Thompson

Christoph wrote:
....
...In my stacktrace i often find the signs "$1" or
"$2" behind a classname... ....
What does it mean?

There's *gold* in them thar' stacktraces!

( OK - see Robert's answer for a technical
explanation - I just couldn't resist ;)

Andrew T.
 
C

Chris Uppal

Christoph said:
"java.lang.ClassCastException: de.gebit.geplan.AllTests$1"

What does it mean? I couldn´t find any documentation on it.

de.gebit.geplan.AllTests$1 is the name of an "anonymous" class defined and used
somewhere inside de.gebit.geplan.AllTests.

Java doesn't /really/ have anonymous classes (or any other kind of nested
class) so the compiler fakes them by creating real classes with made-up names.

-- chris
 
T

Thomas Fritsch

Christoph said:
i have a simple question. In my stacktrace i often find the signs "$1" or
"$2" behind a classname, for example:

"java.lang.ClassCastException: de.gebit.geplan.AllTests$1"

What does it mean? I couldn´t find any documentation on it.
In addition to what Robert and Chris correctly said:

Somewhere in your source AllTests.java you have one or more constructs like
new SomeClassOrInterface()
{
... some methods
}

Roughly speaking, the compiler compiles that to
new AllTests$1(this)
and generates another class
class AllTests$1 extends SomeClassOrInterface
{
final AllTests this$0;
Alltests$1(AllTests outerObject)
{
this$0 = outerObject;
}
... some methods
}
 
I

Ian Wilson

Christoph said:
Hello,

i have a simple question. In my stacktrace i often find the signs "$1" or
"$2" behind a classname, for example:

"java.lang.ClassCastException: de.gebit.geplan.AllTests$1"

What does it mean? I couldn´t find any documentation on it.

The compiler promotes inner classes to normal classes. IIRC this is so
that, when you run the application, the JVM doesn't have to do anything
differently (in this respect) than it did before inner classes were
added to Java.

Classes created this way get a name based on the parent's name plus an
index number plus the inner class name (unless anonymous).
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top