You can use a Static Initialization Block:
class YourClass {
static {
System.out.println("I got loaded!");
}
}
This will be called at the moment the class gets loaded by the JVM.
For the unloading part, a way (neccessarilly not the best) is to start a timer in the static initializer in which you close the resources after a certain time of no usage. Usage would be indicated by a constructor being called or something like that, but it might be tricky to implement with concurrency issues.
Another way could be to write a custom Classloader.