I have a class Person and I'd like to be aware of every call to getFirstName() method:
public class Person {
private String firstName;
private String lastName;
public Person() {...}
public String getFirstName() {
return this.firstName
}
//... Rest of getters and setters
}
So when somebody does this, I'd like to print out "Hello world" for example (actual use would be more complicated):
public class Main {
public static void main(String[] args) {
Person person = new Person("John", "Doe");
person.getFirstName();
}
However I want to do this without modifying Person class (at least NOT before compilation). I read sth about Observable pattern and Property Change Listeners, but they don't seem to be doing what I'd want. Is such thing even possible in java ?