Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Accessing static members with J/Link

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110996] Re: Accessing static members with J/Link
  • From: Raffy <adraffy at gmail.com>
  • Date: Fri, 16 Jul 2010 05:17:35 -0400 (EDT)
  • References: <i1mc8o$8v5$1@smc.vnet.net>

On Jul 15, 12:10 am, John_V <jvillar.j... at gmail.com> wrote:
> I'm trying to learn how to use J/Link, and I have run into an elementary
> (I hope) problem when I try to access static methods and fields. For
> example, the Java Math class has static fields for E and PI and a number
> of static methods, abs() among them. Here's what happens when I try to
> use them:
>
> My inputs:
>
>     Needs["JLink`"]
>     InstallJava[]
>     myMathClass = LoadJavaClass["java.lang.Math"]
>     Fields[myMathClass]
>     pi = myMathClass`PI
>     myMathClass`abs[-5]
>
> /Mathematica/'s outputs:
>
>     Out[2]= LinkObject[...]
>
>     Out[3]= JavaClass[java.lang.Math, <> ]
>
>     Out[4]//TableForm=
>          static final double E
>          static final double PI
>
>     Out[5]= myMathClass`PI
>
>     Out[6]= myMathClass`abs[-5]
>
> I believe the fact that Out[4] correctly lists java.lang.Math's static
> fields means LoadJavaClass worked, but I expected Out[5] = 3.1415... and
> Out[6] = 5. I thought this was a near verbatim transcription of the
> examples in the "Calling Methods and Accessing Fields" part of the
> J/Link user guide. What am I doing wrong?
>
> Thanks,
>
> John

LoadJavaClass (by default) exposes the static members in the context
hierarchy that mirror the Java class name.

ie. java.lang.Math -> java`lang`Math

So if you want java.lang.Math.PI, you can access it from
java`lang`Math`PI


  • Prev by Date: Re: Templates from Usage Messages
  • Next by Date: Re: Select only certain sublists matching criteria
  • Previous by thread: Re: Accessing static members with J/Link
  • Next by thread: Re: Accessing static members with J/Link