Being able to encapsulate members of a class is important for security and integrity. We can protect variables from unacceptable values. The sample code below describes how encapsulation can be used to protect the MyMarks object from having negative values. Any modification to member variable �vmarks� can only be carried out through the setter method setMarks(int mark). This prevents the object �MyMarks� from having any negative values by throwing an exception.
Reflection is used when you have to introspect a class and its memebers to do stuff dynamically. This is used by lot of application frameworks like Hibernate etc, java based editors, etc. Code with intrspection can adversely affect performance proper caching strategy should be implemented.