• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static block and Loading of native library

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class that loads native library in static block. All other methods in the class are NOT static methods - so I need to instantiate the class to call methods on it.

------------------------------------------
class MyClass {

static {
System.loadLibrary("MyLibrary");
}

public native string method1();
}

calling program method:
-------------------------------------------
{
.....
MyClass myClass = new MyClass();
String str = myClass.method1();
myClass = null; //allow it to be garbage collected
......
}

calling program method2:

{
.....
MyClass myClass = new MyClass();
String str = myClass.method1();
myClass = null; //allow it to be garbage collected
......
}

-----------------------------------------------------

Please someone tell me if my understanding is wrong:

The native library will be loaded when the class will be instantiated for the first very time. Even if I set the instance to null and if the object really gets garbage collected, because the native library is loaded from a static block, it still remains loaded and next time I instantiate the class (in calling program method2), it won't load that library again - is this thought process right???

Thanks,
P.Ingle
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The native library will be loaded before (possibly immediately before, but some time before) the class is instantiated, when the class is loaded. The library will never be loaded again...

... unless, of course, the class is loaded a second time by another class loader. What happens under these circumstances used to be murky, at best; I think they may have clarified the situation in Tiger. But in any case, the library will be loaded once, when the class is loaded, and instantiating objects will not cause it to be reloaded, regardless of garbage collection.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic