Quick revision
The short answer
@XmlRootElement marks a class that can be the top element of a document, @XmlElement maps a field to a child element and @XmlAttribute maps it to an attribute, so id becomes <product id="1"> and name becomes <name>. @XmlTransient leaves a field out of the XML, and @XmlAccessorType decides whether JAXB looks at fields or at getters and setters; FIELD is the clearest choice, because the default PUBLIC_MEMBER can map the same value twice. JAXB also needs a no-argument constructor, which may be private, to create objects when unmarshalling.
What to remember
In this answer, we built a Product class and saw what each annotation does: @XmlRootElement lets a class be a document root, @XmlAttribute writes a simple value as an attribute, @XmlElement maps and renames a child element, and @XmlTransient leaves a member out. We saw that @XmlAccessorType decides which members are considered, that FIELD is the clearest choice, and that the default PUBLIC_MEMBER can map a value twice and fail at startup. Finally, we saw that JAXB needs a no-argument constructor, which can be private.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
@XmlRootElement marks a class that can be the top element of a document, @XmlElement maps a field to a child element and @XmlAttribute maps it to an attribute, so id becomes <product id="1"> and name becomes <name>. @XmlTransient leaves a field out of the XML, and @XmlAccessorType decides whether JAXB looks at fields or at getters and setters; FIELD is the clearest choice, because the default PUBLIC_MEMBER can map the same value twice. JAXB also needs a no-argument constructor, which may be private, to create objects when unmarshalling.
Why this question is asked
Most JAXB problems in real projects come from not knowing exactly what the annotations do: an element that disappears, a field that appears twice, an attribute that comes out as an element, or a startup error about duplicate properties. Interviewers ask this concept question to see whether we can predict the XML from the class, which is the skill we need when matching a partner's format. In this answer, we will build a Product class step by step, see what each annotation changes in the output, and then look at access types and the constructor rule that cause the most surprising errors.
1A Quick Recap: What JAXB Maps by Default
JAXB, now Jakarta XML Binding, converts between Java objects and XML. It works out the mapping from our class: every property it considers becomes an XML element with the property's name, unless an annotation says otherwise. A property here means a value JAXB can read and write, either a field or a getter and setter pair. Annotations let us change the name, turn an element into an attribute, leave something out, or choose which members count as properties at all.
2Building the Product Class
Here is the class from the question, with id as an attribute and name as an element, plus a field we want to keep out of the XML.
Notice that price has no annotation and still appears. With FIELD access, every non-static, non-transient field is mapped, so annotations are only needed where we want something other than the default.
3What Each Annotation Does
@XmlRootElement(name = "product") marks Product as a class that can be the root, the outermost element of a document. Without it, JAXB can still map Product when it appears inside another class, but it cannot marshal a Product on its own without being told the element name, and unmarshal() does not know which class a <product> root belongs to. The name attribute sets the element name; without it JAXB uses the class name with a lowercase first letter.
@XmlAttribute on id writes the value as an attribute of the element, id="1", instead of a child element. Attributes can only hold simple values such as numbers, strings and dates, never nested objects. @XmlElement(name = "name", required = true) maps a field to a child element and lets us rename it. required = true does not make JAXB reject a missing value when reading; it mainly affects the schema JAXB generates, where the element gets minOccurs="1". To enforce it, we validate against a schema.
@XmlTransient tells JAXB to ignore the member completely, both when writing and when reading. It is the JAXB equivalent of Java's transient keyword, and we use it for derived values, caches and internal data. On a class, it has a different meaning: the class itself is not mapped, but its properties are folded into its subclasses, which is useful for a base class we do not want to appear in the XML.
| Annotation | On | Result in the XML |
|---|---|---|
@XmlRootElement(name = "product") | class | <product> can be the document root |
@XmlAttribute | id | <product id="1"> |
@XmlElement(name = "name", required = true) | name | <name>Desk lamp</name>, minOccurs=1 in a schema |
| none (FIELD access) | price | <price>29.90</price> |
@XmlTransient | internalNote | not written, not read |
4Access Types and the Double-Mapping Trap
@XmlAccessorType decides which members JAXB considers. FIELD maps every non-static, non-transient field, whatever its visibility, and ignores getters unless annotated. PROPERTY maps every getter and setter pair. PUBLIC_MEMBER, which is the default when we say nothing, maps every public getter and setter pair and every public field, plus anything else we annotate. NONE maps only what we annotate.
The default causes a well-known error. Suppose we keep PUBLIC_MEMBER, annotate a private field, and also have a public getter for it. JAXB now sees two properties with the same name: the annotated field, and the public getter that the default includes.
The fix is to be explicit. Either put @XmlAccessorType(XmlAccessType.FIELD) on the class and annotate fields, or keep annotations only on getters. Most teams choose FIELD, because the mapping is then visible in one place, getters can contain logic without affecting XML, and classes need no setters at all. We can also set the access type for a whole package in package-info.java.
5Why JAXB Needs a No-Argument Constructor
When unmarshalling, JAXB first creates an empty object and then fills in each property as it reads the XML. To create that empty object, it calls a no-argument constructor through reflection. If the class has only a constructor with parameters, JAXBContext creation fails with an error saying the class does not have a no-arg default constructor.
The constructor may be private, as in our Product, because reflection can call it. That keeps the public API clean: application code must use the real constructor, while JAXB uses the private one. For classes we cannot change, or immutable types such as records, the answer is an XmlAdapter that converts to a mutable binding class.
6Follow-up questions
What is the difference between @XmlElement and no annotation?
With FIELD or PROPERTY access, an unannotated member is mapped as an element with its own name anyway. @XmlElement lets us change the name, the namespace, required, nillable and the default value, and with access type NONE it is what makes the member mapped at all.
Can an attribute hold a list?
Yes, if the values are simple: a List<String> with @XmlAttribute and @XmlList becomes a single attribute with space-separated values, such as tags="red blue". Complex objects can never be attributes.
How do I make JAXB omit null values?
It already does: a null element is simply not written. If the partner wants an explicit empty marker, we use @XmlElement(nillable = true), which writes the element with xsi:nil="true".
7Conclusion
In this answer, we built a Product class and saw what each annotation does: @XmlRootElement lets a class be a document root, @XmlAttribute writes a simple value as an attribute, @XmlElement maps and renames a child element, and @XmlTransient leaves a member out. We saw that @XmlAccessorType decides which members are considered, that FIELD is the clearest choice, and that the default PUBLIC_MEMBER can map a value twice and fail at startup. Finally, we saw that JAXB needs a no-argument constructor, which can be private.
Open with
I would explain them on the Product class. @XmlRootElement with name product says this class can be the root element of a document, so JAXB can marshal it on its own and knows which class a product root unmarshals to. @XmlAttribute on id turns it into an attribute, so the output starts with product id="1"; attributes can only hold simple values. @XmlElement with name name maps the field to a child element and lets me rename it; required true mostly affects the generated schema, so to enforce it I validate against an XSD. @XmlTransient excludes a field completely, for example an internal note or a cached value. @XmlAccessorType decides what JAXB looks at.
Then, if they want more
I normally use FIELD, so all non-static fields are mapped and getters can contain logic. The default is PUBLIC_MEMBER, which maps public getters and setters and public fields, and if I annotate a private field and also have a public getter, JAXB sees two properties with the same name and fails at startup.
Finally, JAXB needs a no-argument constructor to create the object before filling it in when unmarshalling. It can be private, so the public API stays clean.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain @XmlRootElement, @XmlElement, @XmlAttribute, @XmlTransient and @XmlAccessorType...": https://scrivio-interview-library.netlify.app/java/jaxb/6b1c082937/