>

Overview

This section explains how you can set and get values on static and instance fields of Java objects from .NET. With Javonet you can access any public field both with value type or reference type.

When working with value type fields you, the value will be automatically converted between .NET and Java types. It means that retrieving value of Integer field you will get .NET integer in results and the same regular int value you can set on any Integer field on Java object.

Working with Static Fields

To access any static field, you need first to retrieve the Java type which declares that field. For that you should call Javonet.getType(“full type name”);. As argument you need to pass full Java type name including package i.e. java.util.Random. In result you will receive an instance of JType which is a handle to Java type.

Next you should just either get or set the value you need using JType.Get(“field name”) or JType.Set(“field name”, value);

In the samples below, we will show how to work with static fields based on this sample Java object:

Sample Java Class:
Class below exposes three static fields. We have reference field which returns another Java class, Integer field and primitive int field that will be used in subsequent examples.

public class SampleJavaClass {

   public static SampleJavaClass staticRefField = new SampleJavaClass();
   public static Integer staticIntField=3;
   public static int staticPrimitiveIntField=4;

}
Retrieving Value from Java Static Field

To retrieve the value you call the generic Get(“field name”) method. As generic type you should pass the type you expect to be returned. For value types it will be .NET counterpart of Java type. In example Int32 for Integer or Int64 for Long. As argument you pass the name of Java field.

Sample .Net Code:

//Activate Javonet ...
//Add reference to your Java package ...
var sampleClassType = JavonetBridge.Javonet.GetType("SampleJavaClass");
JObject objValue = sampleClassType.Get<JObject>("staticRefField");
int intValue = sampleClassType.Get<Int32>("staticIntField");
int primitiveIntValue = sampleClassType.Get<Int32>("staticPrimitiveIntField");

Please notice that regardless if your Java value-type field is boxed i.e. Integer or unboxed i.e. int the regular .NET integer will be returned.

Setting Value on Java Static Field

Setting value on Java static field from .NET is very straightforward all you need to do is call Set(“field name”, value) method. As first argument you provide the field name and value as second argument. For value type fields the value can be passed as regular .NET value type and will be automatically converted. For reference type fields you set an instance of JObject which represents your instance of Java object you want to set.

Sample .Net Code:

//Activate Javonet ...
//Add reference to your Java package ...
JObject javaRandomObjInstance = JavonetBridge.Javonet.New("java.util.Random");
var sampleClassType = JavonetBridge.Javonet.GetType("SampleJavaClass");
sampleClassType.Set("staticRefField", javaRandomObjInstance);
sampleClassType.Set("staticIntField", 2);
sampleClassType.Set("staticPrimitiveIntField", 5);

Please notice that regardless if your Java value-type field is boxed i.e. Integer or unboxed i.e. int the regular .NET integer can be set as value. It is possible because it is not possible in Java to define two fields with the same name but boxed and unboxed primitive type. Therefore, there is no need to differentiate the set method by explicitly specifying argument type.

Working with Instance Fields

To access any instance filed you need to create an instance of Java object first. For that you call Javonet.New(“full type name”);. As argument you need to pass full Java type name including package i.e. java.util.Random. In result you will receive an instance of JObject which is a handle to Java object instance.

Next you should just either get or set the value you need using JType.Get(“field name”) or JType.Set(“field name”, value);

In the samples below, we will show how to work with instance fields based on this sample Java object:

Sample Java Class:
Class below exposes three instance fields. We have reference field which returns another Java class, Integer field and primitive int field which will be used in subsequent examples.

public class SampleJavaClass {

	public SampleJavaClass instanceRefField = new SampleJavaClass();
	public Integer instanceIntField=5;	
	public int instancePrimitiveIntField=6;

}
Retrieving Value from Java Instance Field

To retrieve the value you call the generic Get(“field name”) method. As generic type you should pass the type you expect to be returned. For value types it will be .NET counterpart of Java type. In example Int32 for Integer or Int64 for Long. As argument you pass the name of Java field.

Sample .Net Code:

//Activate Javonet ...
//Add reference to your Java package ...
var sampleObj = JavonetBridge.Javonet.New("SampleJavaClass");
JObject objValue = sampleObj.Get<JObject>("instanceRefField");
int intValue = sampleObj.Get<Int32>("instanceIntField");
int primitiveIntValue = sampleObj.Get<Int32>("instancePrimitiveIntField");

Please notice that regardless if your Java value-type field is boxed i.e. Integer or unboxed i.e. int the regular .NET integer will be returned.

Setting Value on Java Instance Field

Setting value on Java instance field from .NET is very straightforward all you need to do is call Set(“field name”, value) method. As first argument you provide the field name and value as second argument. For value type fields the value can be passed as regular .NET value type and will be automatically converted. For reference type fields you set an instance of JObject which represents your instance of Java object you want to set.

Sample .Net Code:

//Activate Javonet ...
//Add reference to your Java package ...
JObject javaRandomObjInstance = JavonetBridge.Javonet.New("java.util.Random");
var sampleObj = JavonetBridge.Javonet.New("SampleJavaClass");
sampleObj.Set("instanceRefField", javaRandomObjInstance);
sampleObj.Set("instanceIntField", 2);
sampleObj.Set("instancePrimitiveIntField", 5);

Please notice that regardless if your Java value-type field is boxed i.e. Integer or unboxed i.e. int the regular .NET integer can be set as value. It is possible because it is not possible in Java to define two fields with the same name but boxed and unboxed primitive type. Therefore, there is no need to differentiate the set method by explicitly specifying argument type.

Download this article

Similiar Articles