Menu

Create_a_property_in_Java

Small SQL

{{DeveloperGuide}}

Java does not support the concept of properties like C#. If you write a .NET library than it can be nice to have properties in your public API.

Properties via map.xml

At compile time you can use a map.xml to modify the compiled result. The follow sample demonstrate the use of map.xml to define a property.

public class IntList{
 private int[] list;
 public IntList(int size){
   list = new int[size];
 }
 public void setItem(int index, int value){
   list[index] = value;
 }
 public int getItem(int index){
   return list[index];
 }
}


<root>
  <assembly>
    <class name="IntList">
      <attribute type="System.Reflection.DefaultMemberAttribute, mscorlib" sig="(Ljava.lang.String;)V">
        <parameter>Item</parameter>
      </attribute>
      <property name="Item" sig="(I)I">
        <getter name="getItem" sig="(I)I" />
        <setter name="setItem" sig="(II)V" />
      </property>
    </class>
  </assembly>
</root>

When IntList.java is compiled to IntList.class and then ikvmc'ed using:

  ikvmc IntList.class -remap:map.xml

The resulting IntList.dll will be usable from C# like this:

  IntList l = new IntList(10);
  l[4] = 42;
  Console.WriteLine(l[4]);

Valdemar Mejstad created a tool to automatically generate the xml to define properties based on Java's java.beans.BeanInfo. The source is available here: MapFileGenerator.java.

Properties via Annotation

If you want create a property for a class member then you can use the annotation.

@ikvm.lang.Property(get = "get_Handle")
private long Handle;

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.