Accessing static members
Sundararajan Athijegannathan
sundararajan.athijegannathan at oracle.com
Wed Jul 13 08:22:50 UTC 2016
Two things:
1) accessString function should be passed with "type" object obtained
from Java.type [or Packages object]. i.e., It can not receive an
instance of a Java class A or B.
Nashorn does not allow static field to be accessible via objects. Only
instance fields and methods are accessible via objects.
2) Even if you pass Java.type("A") or Java.type("B") to accessString
function, only the static fields, methods of *exact* type are accessible
via "type" objects.
i.e., inherited static fields, methods are not accessible. Only static
members of the exact Java type are accessible via type object.
Hope this helps,
-Sundar
On 7/13/2016 1:09 PM, Axel Dörfler wrote:
> Hi there,
>
> I've tried something like the following code:
> Java:
>
> public abstract class A {
> public static final String STRING = "TEXT";
> }
>
> public class B extends A {
> public static final String ANOTHER = "TEST";
> }
>
> JavaScript:
>
> function accessString(instanceOfB) {
> print(instanceOfB.STRING);
> print(instanceOfB.ANOTHER);
> }
>
> Expected output would be:
> TEXT
> TEST
>
> Actual output is:
> undefined
> TEST
>
> I guess this is a bug?
>
> Bye,
> Axel.
More information about the nashorn-dev
mailing list