Final Fields Specifics

This topic applies to java version only 

Db4o uses reflection to store and retrieve objects from the database file. In the case of final fields db4o needs a successful call to java.lang.Field#setAccessible to allow write access to those fields. Unfortunately different Java versions produce different results in this case. To be more specific:

You can use the following example code to check final fields behavior with different java versions:

TestFinal.java
01package com.db4odoc.finalfields; 02import java.io.File; 03 04import com.db4o.Db4o; 05import com.db4o.ObjectContainer; 06import com.db4o.ObjectSet; 07 08public class TestFinal 09{ 10 // non-final fields 11 public int _i; 12 public String _s; 13 // final fields storing the same values as above 14 public final int _final_i; 15 public final String _final_s; 16 17 public static void main(String[] args) 18 { 19 new File("test.yap").delete(); 20 ObjectContainer db = Db4o.openFile("test.yap"); 21 try { 22 TestFinal test = new TestFinal(1,"test"); 23 db.set(test); 24 System.out.println("Added: " + test); 25 } finally { 26 // Close does implicit commit and refreshes the reference cache 27 db.close(); 28 } 29 db = Db4o.openFile("test.yap"); 30 try { 31 ObjectSet result = db.get(null); 32 listResult(result); 33 } finally { 34 db.close(); 35 } 36 } 37 // end main 38 39 public TestFinal(int i, String s) 40 { 41 // initialize final and non-final fields with the same values 42 _i = i; 43 _s = s; 44 _final_i = i; 45 _final_s = s; 46 } 47 // end TestFinal 48 49 public String toString() 50 { 51 return "Int - " + _i + "; String - " + _s + "; FINAL Int - " + _final_i + "; FINAL String - " + _final_s; 52 } 53 // end toString 54 55 public static void listResult(ObjectSet result) 56 { 57 while(result.hasNext()) { 58 System.err.println(result.next()); 59 } 60 } 61 // end listResult 62}

If you are using Eclipse it is easy to switch between java versions - you can switch to the versions lower than the one installed on your computer without having to install them all. For example if you are using JDK6 you can easily test your project on JDK1.1 - 1.4 and JDK5. Just go to the project properties, select "Java Build Path" on the left panel and "Libraries" tab on the right panel. Remove the system library currently used. Select "Add library->JRE System Library"; on the next screen check the "Execution Environment" radio button and select the desired environment from the list.

Don't forget to use the appropriate db4o version for the selected java environment version. See db4o on Java Platformsupdated for more information.