Anonymous classes mess with field access from JS in unexpected ways

Marc Downie marc at
Fri Jul 25 22:48:05 UTC 2014

Accessing to fields (but not methods) of anonymous classes and their
superclasses seems broken, to me (1.9.0-ea-b23 & 1.8.0_20-ea-b23). Example:

package somepackage;

public class ScratchClass {

public class ClassOne {
public String peach = "pear";

public String banana() {
return "banana";

public ClassOne c2 = new ClassOne() {
public String yetMoreMember = "yet more";

public String banana() {
return "banana, overridden in c2";

public String pear() {
return "pear";

public ClassOne c1 = new ClassOne();


jjs> var sc = new (Java.type("somepackage.ScratchClass"))()
jjs> sc
somepackage.ScratchClass at 418e7838 *// expected*
jjs> sc.c1
somepackage.ScratchClass$ClassOne at 4f209819 *// expected*
jjs> sc.c2
somepackage.ScratchClass$1 at 7193666c *// expected*
jjs> sc.c1.peach
pear  *// expected*
jjs> sc.c1.banana()
banana  *// expected*
jjs> print(sc.c2.banana())
banana, overridden in c2  *// expected --- refers to overridden method*

So far so good. But:

jjs> print(sc.c2.peach)
undefined *// unexpected --- should refer to field in superclass ClassOne?*
jjs> print(sc.c2.yetMoreMember)
undefined *// unexpected --- should refer to field in anon class?*
jjs> print(sc.c2.pear())
<shell>:1 TypeError: somepackage.ScratchClass$1 at 7193666c has no such
function "pear"
*// unexpected --- should refer to method in anon class?*

If I give that anonymous subclass of ClassOne an actual name and
instantiate that instead all is well.

Bug or misunderstanding about the Java/JavaScript interface?

many thanks,


More information about the nashorn-dev mailing list