title: What is the efficient way in writing a java card applet?
body: <p>With regard to absence of the Garbage Collector, I'm confused that what is the best way to define fields and variables?
  We know that in Java Card technology those objects that allocated with with <code>new</code> keyword, became unreachable when go out of the scope, but the storage space they occupy will never be reclaimed. </p>
  
  <p>Now, assume that I want to send 'Hello' in reception of each APDU command. </p>
  
  <p>What is the difference between these implementations (Memory and CPU consumption)? Which one is best and why?</p>
  
  <p>1- </p>
  
  <pre><code>public class SimpleApp extends Applet {
      byte[] hello = { (byte) 'H', (byte) 'e', (byte) 'l', (byte) 'l', (byte) 'o' };;
  
      private SimpleApp() {
      }
  
      public static void install(byte bArray[], short bOffset, byte bLength)
              throws ISOException {
          new SimpleApp().register();
      }
  
      public void process(APDU arg0) throws ISOException {
          byte[] buffer = arg0.getBuffer();
          Util.arrayCopyNonAtomic(hello, (short) 0, buffer, (short) 0, (short) 5);
          arg0.setOutgoingAndSend((short) 0, (short) 5);
      }
      }
  </code></pre>
  
  <p>2- </p>
  
  <pre><code>public class SimpleApp extends Applet {
      static byte[] hello = { (byte) 'H', (byte) 'e', (byte) 'l', (byte) 'l', (byte) 'o' };;
  
      private SimpleApp() {
      }
  
      public static void install(byte bArray[], short bOffset, byte bLength)
              throws ISOException {
          new SimpleApp().register();
      }
  
      public void process(APDU arg0) throws ISOException {
          byte[] buffer = arg0.getBuffer();
          Util.arrayCopyNonAtomic(hello, (short) 0, buffer, (short) 0, (short) 5);
          arg0.setOutgoingAndSend((short) 0, (short) 5);
      }
  
  }
  </code></pre>
  
  <p>3-</p>
  
  <pre><code>public class SimpleApp extends Applet {
      byte[] hello = { (byte) 'H', (byte) 'e', (byte) 'l', (byte) 'l', (byte) 'o' };;
  
      private SimpleApp() {
      }
  
      public static void install(byte bArray[], short bOffset, byte bLength)
              throws ISOException {
          new SimpleApp().register();
      }
  
      public void process(APDU arg0) throws ISOException {
          byte[] buffer = new byte[255];
          buffer = arg0.getBuffer();
          Util.arrayCopyNonAtomic(hello, (short) 0, buffer, (short) 0, (short) 5);
          arg0.setOutgoingAndSend((short) 0, (short) 5);
      }
  
  }
  </code></pre>
  
  <p>4- </p>
  
  <pre><code>public class SimpleApp extends Applet {
      static byte[] hello = { (byte) 'H', (byte) 'e', (byte) 'l', (byte) 'l', (byte) 'o' };;
      byte[] buffer;
      private SimpleApp() {
      }
  
      public static void install(byte bArray[], short bOffset, byte bLength)
              throws ISOException {
          new SimpleApp().register();
      }
  
      public void process(APDU arg0) throws ISOException {
          buffer = arg0.getBuffer();
          Util.arrayCopyNonAtomic(hello, (short) 0, buffer, (short) 0, (short) 5);
          arg0.setOutgoingAndSend((short) 0, (short) 5);
      }
  
  }
  </code></pre>
  
  <p>5-</p>
  
  <pre><code>public class SimpleApp extends Applet {
      static byte[] hello = { (byte) 'H', (byte) 'e', (byte) 'l', (byte) 'l', (byte) 'o' };;
      static byte[] buffer;
      private SimpleApp() {
      }
  
      public static void install(byte bArray[], short bOffset, byte bLength)
              throws ISOException {
          new SimpleApp().register();
      }
  
      public void process(APDU arg0) throws ISOException {
          buffer = arg0.getBuffer();
          Util.arrayCopyNonAtomic(hello, (short) 0, buffer, (short) 0, (short) 5);
          arg0.setOutgoingAndSend((short) 0, (short) 5);
      }
  
  }
  </code></pre>