Java tutorial
//package com.java2s; import java.util.HashMap; import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; public class Main { private static Map<Class<?>, JAXBContext> contextCache = new HashMap<>(); /** * Returns the corresponding {@link JAXBContext} for the given {@link Class}. * * @param clazz {@link Class} * @return {@link JAXBContext} * @throws JAXBException */ private static <T> JAXBContext getContext(Class<T> clazz) throws JAXBException { synchronized (contextCache) { if (!contextCache.containsKey(clazz)) { contextCache.put(clazz, JAXBContext.newInstance(clazz)); } } return contextCache.get(clazz); } }