title: Get unread messages from array with all email messages in Java
body: <p>I am trying to write a method that gets an array of unread mail messages from an array with all messages. This is my code:</p>
  
  <pre><code>public Message[] getUnreadMessages() {
      Message[] messages = inbox.getMessages(1, messageCount);
      Message[] unreadMessages;
      int i = 0;
      for (Message message : messages) {
          boolean isMessageRead = false;
          for (Flags.Flag flag : message.getFlags().getSystemFlags()) {
              if (flag == Flags.Flag.SEEN) {
                  isMessageRead = true;
                  break;
              }
          }
          String subject = message.getSubject();
          if (isMessageRead) {
              log.debug("Message with subject '" + subject + "' has status read");
          } else {
              unreadMessages[i] = new Message();
              unreadMessages[i] = message;
              i++;
              log.info("Message with subject '" + subject + "' will be processed");
          }
      }
      return unreadMessages;
  }
  </code></pre>
  
  <p>However, this doesn't work because I cannot instantiate a new Message() type. How can I solve this?</p>