title: Marshal inner class with JAXB - Java
body: <p>I followed the following tutorial on JAXB <a href="http://blog.adamsbros.org/2010/02/07/jaxb-example-code/" rel="nofollow">http://blog.adamsbros.org/2010/02/07/jaxb-example-code/</a>, and wanted to modify it to include the class I want to as an nested (inner) class as follows:</p>
  
  <pre><code>import javax.xml.bind.JAXBContext; 
  import javax.xml.bind.JAXBException; 
  import javax.xml.bind.annotation.*; 
  import java.io.StringReader; 
  import java.io.StringWriter; 
  
  public class HighlevelClass
  {
  
  
  @XmlRootElement(name = "employee")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Employee
  {
  
      @XmlAttribute
      public int id;
  
      @XmlElement
      public
      String firstName;
  
      @XmlElement
      public
      String middleName;
  
      @XmlElement
      public
      String lastName;
  
      public Employee()
      {
      }
  
  }
  
  public static void main(String args[]) throws JAXBException
  {
  
      HighlevelClass HClass = new HighlevelClass();
      Employee john = HClass.new Employee();
      john.firstName = "John";
      john.middleName = "Robert";
      john.lastName = "Doe";
      john.id = 1;
  
      // write it out as XML
      final JAXBContext jaxbContext = JAXBContext.newInstance(HighlevelClass.Employee.class);
  }
  </code></pre>
  
  <p>But it's giving an eror in the last line when I try to make the JAXB context. I tried number of combinations but I only got it to compile when I made Employee class static.
  Can somebody please help me understand whats going wrong.</p>
  
  <p>Thank you.</p>