I am studying for the OCA Exam and came across this code:
public class Driver {
private void printColor(String color) {
color = "purple";
System.out.print(color);
}
public static void main(String[] args) {
new Driver().printColor("blue");
}
}
The question asks "What is the outcome of this piece of code". I initially thought it will be "it does not compile" because you have an object instance trying to access a private method. But, it turns out to be "purple".
Why is it "purple" and not "it does not compile"? I know the Driver
instances lives in the same class it is declared in, but why it still have the privilege to access private methods?
Thank you