1

I have read everywhere that JAXBContext.newInstance(SampleClass.class) will load the SampleClass and create a JAXBContext object from it. For example, last answer here and there were blogs as well.

But when I do following then I do not see class getting loaded 10000 times. I verified this using a profiling tool that SampleClass is not loading 10000 times.

   for (int i = 0; i < 10000; i++) {
        JAXBContext jaxbContext = JAXBContext.newInstance(TestData.class);
        jaxbContext.hashCode();
        jaxbContext.toString();
        jaxbContext.getClass();
    }

Is my understanding about JAXBContext.newInstance wrong?

I know that if a class is loaded by JVM then it will not be loaded again but this is what I have read everywhere that using JAXBContext.newInstance() will load the class each time.


EDIT: Here is more observation from jVisualVM, I invoked it ~10000 but only ~200-300 class loading happened, and then 174 classes were shown as unloaded.
Community
  • 1
  • 1
pjj
  • 2,015
  • 1
  • 17
  • 42
  • The JVM loads a class once (as far as I am aware), hence the static{} block often used to do actions on first load of a class. Doesn't newInstance inherently mean a new *object*? – Sameer Puri Aug 03 '16 at 21:05
  • @SameerPuri Yes, you are right that JVM loads class only once, but this more question about how `JAXBContext` works in terms of class loading because it possible to load class many times even if it is already loaded. `JAXBContext.newInstance()` is a like a factory method to get the new instance of the passed class but before the instance is created, whether class will be loaded again or not is the question. – pjj Aug 27 '16 at 21:03

0 Answers0