How to make any private field/method public in java

From the beginning of java learning we have been taught about the scope of private fields/methods in java but despite the fact about private fields/methods accessibility outside the class, its achievable using java’s reflection library. So let’s today discuss how we can achieve this using reflection.

Let’s consisted following class contain one private field and one private method.

The above class has a method computeMsgLength() which is private to the class. Now if we want to access this method out site of the class/package then we need to use java reflection api.

To make any field/method accessible from outside its scope, use setAccessiable() method of Field/Method class.

About the Author

has written 69 posts.

I Love Coding, Analyzing, Designing and doing it the right way. I am obsessed about learn anything new daily and sharing what I have learn each Day. This Blog is journey of my Zeal, going through step by step to the mountain of desire to become prefect. Google Wanna Hire me ?

Copyright © 2020 All rights reserved.
Proudly powered by WordPress. Developed by 7Tech Solutions