0

I am creating a REST service with Jersey. Some resources (like some global Maps, files) should be loaded before the first request arrives (because loading this resources need 20 secs). So I create some static fields (attributes) to hold these resources, because I believe they should be initialized when I start this REST service.

However, I found one strange problem that static resources will be loaded only when I send the first request. So for the first request, it always takes more than 20s to get response, while later requests are much faster (since resources have been loaded).

Could anyone explain me why Java doesn't initialize these static fields before any request ?

Ensom Hodder
  • 1,522
  • 5
  • 18
  • 35

1 Answers1

1

In Java, classes are initialized the first time they are loaded. It is your first request that causes the classes to load.

Maybe this will help.

Community
  • 1
  • 1
Anton Arhipov
  • 6,479
  • 1
  • 35
  • 43
  • Yes, I agree with you on the Java initialization problem. further your referral link should be very close to my problem, I will check soon. Thanks very much ! – Ensom Hodder Aug 27 '12 at 12:24