Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflecting on Java [duplicate]

I just messed up an opportunity, failing to answer 2 questions. I still do not know the answers, so looking for them:

[1] You have a Java class with private variables and no getter/setter methods. How do you modify such variables?

My answer: You cannot do it, private variables cannot be accessed from outside. Interviewer: Correct answer is "Using reflection".

[2] Which reflection methods do you use to do the above? My answer: I am not sure. Interviewer: Good bye.

From my experience, I'd (1) Check if the class exists (2) Create an instance (3) Check if a method exists (4) Invoke the method (5) Carry on with the instance of class. Of course, I'd catch Exceptions like ClassNotFound and MethodInvocation. But is there a trick to modify private variables? And do people do this? TIA.

like image 240
Manidip Sengupta Avatar asked Oct 22 '22 15:10

Manidip Sengupta


2 Answers

Given this:

You have a Java class with private variables and no getter/setter methods. How do you modify such variables?

my response would be that you don't need specific setters/getters and you'd just modify them in other non-specific methods. Setters/getters can be viewed in many cases as exposing the implementation.

In order to make a field accessible, you have to call Field.setAccessible().

It's the sort of topic I don't know off the top of my head, and have to look up if/when I use it (I can't remember when I last used it). For an interviewer to be so hung up on this seems a little unusual.

like image 176
Brian Agnew Avatar answered Nov 03 '22 19:11

Brian Agnew


Well, even if a class does not have getters and setts it does not mean there are no other methods in the class that could modify its state. After all if they are in the class is because the class needs them to carry out any kind of task. So, chances are that other methods could modify the state of a class as part of their responsibility.

For instance, if your class was a bank account class, probably there is no method to set the balance of an account directly, but maybe there are methods that accept transactions like withdrawals or deposits which would ultimately modify the state of the class internally.

So this is really a bad question from the begining, because without enough context the answer is difficult. Reflection could be an option, but if you are using a security manager, that may prevent reflection from modifying private fields, so that may also be a wrong answer.

So, you see, the question is not good enough and without context it makes no sense. Based on the detail that you were given, perhaps the answer to the question should have been another question like:

Why would you like to modify the private fields of a class? After all they are private for a reason. So, basically if you find you need to change them, what you need to do is to sit down and reconsider you design and determine if your class does not have the proper public interface.

That answer could lead the interviewer to give you more context and then you can provide the appropriate answer. For instance, now the interviewer could say that the class is part of a third-party framework and you do not have the source code for it, so it is not possible to provide getters/setter methods but you are forced to read/wrtite some private state of a given object of this framework as workaround to solve some issue.

Now, you would have more context and you could ask another question, like: are you using a security manager on this application? If he/she says no, then you can say, well in that case, reflection is an option.....

like image 31
Edwin Dalorzo Avatar answered Nov 03 '22 19:11

Edwin Dalorzo