ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Reflection and Class loading

by Daniel H. Steinberg
ONJava Newsletter for 11/14/2003


I am headed to ApacheCon next week. Of course, not all of the Apache projects are relevant to Java developers, but quite a few are central to the work we do each day. If there are Apache projects or other interesting Java projects that you'd like to see us cover, please drop me a line. Historically, we tend to focus more on open source projects here on OnJava.

There are good reasons for making variables and methods private in your Java code. In his article Subverting Java Access Protection for Unit Testing, Ross Burton says that you do not want to make a member either public or package level just to expose it to unit tests. Some members of the TDD community would counter that you should not need to test private members as they are not part of a class' public interface. The issue is undecided and so it is interesting to consider Burton's use of reflection to access these private members. First, he demonstrates that even when using the Reflection APIs, private fields are generally inaccessible. Then he leads you through the steps required to sidestep the access control and be able to see and call private fields, methods, and constructors. In the article's feedback, an anonymous reader suggests the use of static inner classes to hold your tests. This gives them access to the private members in the enclosing class.

Andreas Schaefer takes you on "an exciting journey into the darker corners of Java" in Inside Class Loaders. The first time you write a class loader there is a new world you need to think about. Schaefer writes, "Additional class loaders enable a Java developer to write modular applications where the visibility of classes is restricted, and therefore, multiple class types can be loaded and managed. Nevertheless, it requires effort to understand the used class loaders and the organization of the classes and class loaders."

To subscribe to the ONJava.com newsletter (or any O'Reilly Network newsletters), visit https://epoch.oreilly.com/account/default.orm and select the newsletters you wish to receive in your user profile (you'll need to log in with your existing O'Reilly Network account -- if you don't yet have an account, you'll need to create one).

To change your newsletter subscription options, please visit https://epoch.oreilly.com/account/default.orm and click the"Manage My Newsletters" link. For assistance, send help to

Erik Hatcher continues his Lucene series on java.net. This week we feature his article on the QueryParser. If your search engine needs to handle human-generated queries, then it needs to make sense of them. Hatcher concludes that QueryParser "is a powerful and relatively flexible way to allow users to enter rich queries into your application. It is designed for human-entered queries. For queries that originate from your application code, it is best to use Lucene's Query API instead."

Till next week,

Daniel H Steinberg, editor
ONJava.com and java.net

ONJava.com News and Weblogs

java.net Weblogs

Other Java News (channel -- LANG: JAVA)

Java Cookbook Recipe of the Day

Java Events

Other O'Reilly Network sites

O'Reilly Network



O'Reilly and Associates

O'Reilly Java

ONJava.com Affiliates



Return to list of ONJava Newsletters.

Return to the ONJava.com.