To easily enable logging for plurality of my project classes, I have decided to abuse the new default
keyword to create a simple method trait for my classes:
default void Log(Level lvl, String msg) {
Logger log = Logger.getLogger(this.getClass().getName());
//Log something
}
What I really dislike about this is the need to get the log
every time. Were I working with C++, I'd declare:
static Logger log = ...;
Every other call to the function, the logger would already be initialized in the variable. In normal classes, I use this pattern to simulate static variable:
class A {
//By default, this is null until needed
private Obj cached_obj = null;
public void doSomethingWithObj(Something thing) {
//Once needed, it only initialises once
if(cached_obj==null)
cached_obj = Obj.generateObj();
cached_obj.doSomething(thing);
}
}
But this is not possible with interface. Interface cannot have any properties.
So is there some other workaround, or is Java going to hold my performance back again?