Suppressing Access Restrictions

There are times when your Reflection code must access private and protected members of another class. Although that sounds like a violation of good object-oriented principles, it's actually a reasonable thing to do when you're writing a library that does data mapping (such as serialization or object-to-relational mapping).

The reason you normally create access restrictions is to keep other classes from becoming dependent on parts of your class that are implementation specific. For example, if you use a counter to keep track of the number of items in an order, you're better off creating a getItemCount method rather than exposing the counter to any client classes. Otherwise, you'd be obligated to keep the counter, ...

Get Special Edition Using Java™ 2 Enterprise Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.