The java.lang ref  

Packages  Java defines two sub packages of java.lang: java.lang.ref and java.lang reflect, Each is briefly described here..

java.lang, ref

You learned earlier that the garbage collection facilities in Java automatically determine when no references.exist to an object. The object is then assumed to be no longer needed and its memory is reclaimed. The classes in the java.lang.ref package, which -was added by Java 2, provide more flexible control over the garbage collective process. For example, assume that your program has created numerous objects that you want to reuse at some later time. You can continue to hold references to.these objects, but that may n-quin- too much masonry. Instead, you Fulfill”so references to these objects. An object that is “softly reachable” can be reclaimed by the garb,age collector.If available memory runs low. In that case, the garbage collector sets the “soft” references to that object to null. Otherwise, the garbage collector saves the object for possible future use. A programmer has the ability to determine whether a “softly reachable” object has been reclaimed, If it has been reclaimed, it can be re-created. Otherwise, H~ object is still available for reuse. You may also create “weak” and “phantom” references to objects. Discussion of these’ and other features of the java.lang ref package are beyond. the scope of this’book.


Reflection is the ability of a program to analyze itself. The java.lang.reflect package t provides the ability it> obtain information about the fields, constructors, methods, and modifiers of a class: You need this information to build software tools that enable you . to work with Java Beans components. The tools use reflection to determine dynamically the characteristics of a component. This topic is considered in Chapter 25. In addition, the java.lang reflect package includes a class that enables you to create and access arrays dynamically.

[vfb id=1]

Share This