Access private member in Java

java

Solution

You can't, unless you use reflection (more about that below).

If the member is declared `private`, then other classes aren't allowed to read it. That's what private scope is about.

If you can modify the class that owns the ArrayList, you can add a getter. It is better not to get the ArrayList in it's entirety (it's a reference and you might inadvertently change it). Add a getter that gets one element, instead.

There is also the option to use reflection, but as is mentioned several times on this page, this is strongly discouraged. Reflection is a technology that allows code to inspect other code in the same system. Should you really want to use it, the tutorial is here.

Problem

I am attempting to access a private member from another class in Java. Is this a possible task? Currently there is no getter method for this member. Below is an image of what I would like to obtain;

Original source

Related problems