O'Reilly logo

Core Java® Volume II—Advanced Features, Ninth Edition by Gary Cornell, Cay S. Horstmann

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

12.4.2. Accessing Static Fields

Accessing static fields is similar to accessing nonstatic fields. Use the GetStaticFieldID and GetStaticXxxField/SetStaticXxxField functions that work almost identically to their nonstatic counterparts, with two differences:

• As you have no object, you must use FindClass instead of GetObjectClass to obtain the class reference.

• You have to supply the class, not the instance object, when accessing the field.

For example, here is how you can get a reference to System.out:

/* get the class */ jclass class_System = (*env)->FindClass(env, "java/lang/System"); /* get the field ID */ jfieldID id_out = (*env)->GetStaticFieldID(env, class_System, "out",    "Ljava/io/PrintStream;"); /* get ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required