As the very simplest approach, remember you can always call this.getClass().getName():
class SimpleCalculator {
public int add(int a, int b) {
System.out.println(this.getClass().getName() +
" - adding " + a + " and " + b);
return a + b;
}
}
Another common approach is to use a logging library like Log4J.
The class you'd be using is Logger
You configure Log4J to write to a certain file.
Each class declares a Logger object that prints messages to a file.
Each message begins with the name of the class that generated the message.
class SimpleCalculator {
Logger calcLogger = Logger.getLogger(SimpleCalculator.class);
public int add(int a, int b) {
calcLogger.debug("add - adding " + a + " and " + b);
return a + b;
}
}
... or you could use a method like @urir suggests.
... or you could get crazy and use AOP.