Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Name mangling
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Creating unique names for inner and anonymous classes==== The scope of anonymous classes is confined to their parent class, so the compiler must produce a "qualified" public name for the [[inner class]], to avoid conflict where other classes with the same name (inner or not) exist in the same namespace. Similarly, anonymous classes must have "fake" public names generated for them (as the concept of anonymous classes only exists in the compiler, not the runtime). So, compiling the following Java program: <syntaxhighlight lang="java"> public class foo { class bar { public int x; } public void zark () { Object f = new Object () { public String toString() { return "hello"; } }; } } </syntaxhighlight> will produce three '''.class''' files: * '''foo.class''', containing the main (outer) class ''foo'' * '''foo$bar.class''', containing the named inner class ''foo.bar'' * '''foo$1.class''', containing the anonymous inner class (local to method ''foo.zark'') All of these class names are valid (as $ symbols are permitted in the JVM specification) and these names are "safe" for the compiler to generate, as the Java language definition advises not to use $ symbols in normal java class definitions. Name resolution in Java is further complicated at runtime, as [[fully qualified name]]s for classes are unique only inside a specific [[classloader]] instance. Classloaders are ordered hierarchically and each Thread in the JVM has a so-called context class loader, so in cases where two different classloader instances contain classes with the same name, the system first tries to load the class using the root (or system) classloader and then goes down the hierarchy to the context class loader.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)