<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Possible Solutions </title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../../../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../../../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../../../SkinSupport/MadCapAll.js"> </script> <script src="../../../Resources/Code/prettify.js"> </script> <script src="../../../Resources/Code/lang-vb.js"> </script> <script src="../../../Resources/Code/jquery.min.js"> </script> <script src="../../../Resources/Code/ZeroClipboard.js"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../../index_CSH.html#advanced_topics/type_handling/final_fields/possible_solutions.htm" style="">Open topic with navigation</a> </p> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1>Possible Solutions</h1> <p>Of course, if you only use JDK5 or 6 there are no worries about the final fields at all. But if you do not want to stick to the definite java version and need to have the flexibility of switching to different java versions you currently have 2 solutions:</p> <ul> <li value="1">avoid using the final modifier in the persistent objects;</li> <li value="2">use translator.</li> </ul> <p>An example of the final fields translator can look like this:</p> <pre class="prettyprint" xml:space="preserve">FinalFieldTranslator.java /**//* Copyright (C) 2007 Versant Inc. http://www.db4o.com */ package com.db4odoc.finalfields; import com.db4o.*; import com.db4o.config.*; // <span class="MCPopup"><a href="javascript:void(0);" class="MCPopupSpot" onclick="FMCPopup( event, this ); return false;" MadCap:src="../translators.htm">Translator<img style="border: none;margin-left: 5px;" src="../../../SkinSupport/ExpandingClosed.gif" MadCap:altsrc="../../../SkinSupport/ExpandingOpen.gif" class="MCExpandingIcon" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../../SkinSupport/ExpandingOpen.gif' ); }" /></a></span> allowing to store final fields on any Java version public class FinalFieldTranslator implements ObjectConstructor { public Object onStore(ObjectContainer container, Object applicationObject) { System.out.println("onStore for " + applicationObject); TestFinal notStorable = (TestFinal) applicationObject; // final fields values are stored to an array of objects return new Object[] { new Integer(notStorable._final_i), notStorable._final_s }; } public Object onInstantiate(ObjectContainer container, Object storedObject) { System.out.println("onInstantiate for " + storedObject); Object[] raw = (Object[]) storedObject; // final fields values are restored from the array of objects int i = ((Integer) raw[0]).intValue(); String s = (String) raw[1]; return new TestFinal(i, s); } public void onActivate(ObjectContainer container, Object applicationObject, Object storedObject) { System.out.println("onActivate for " + applicationObject + " / " + storedObject); } public Class storedClass() { return Object[].class; } }</pre> <p>The following call should be issued before opening the object container to connect the translator to the TestFinal class:</p> <p><code>configuration.common().objectClass(TestFinal.class).translate(new FinalFieldTranslator());</code> </p> <p MadCap:conditions="Primary.Online">Download example code:</p> <script type="text/javascript" src="../../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>