Reputation: 89
Java logging API is used in my project. They are using logger like in constructor of a class A, for example:
public A(Context context) {
log_ = (Logger) context.getAttribute(LOGGER);
}
I have to implement it for a class which doesnot have a constructor... I tried to make a object of that class like in:
Class B {
B b;
}
and tried to use logger like:
log_ = (Logger) b.getAttribute(LOGGER);
But I keep getting the error <identifier expected>
at this line? What is fault here? Thanks in advance.
Upvotes: 0
Views: 918
Reputation: 20885
You can't put arbitrary statements directly in a class definition (in fact it's a definition). You can initialize your member fields
getLogger()
All three options illustrated in (valid) Java code:
class B {
Context ctx = Context.getDefault();
Logger log = ctx.getLogger();
B(Context ctx) {
log = ctx.getLogger();
}
Logger logger() {
return ctx.getLogger();
}
}
Upvotes: 1