2

I am looking at some code and have seen a lot of way to define the log instance in the class.here are few of them

protected static final Logger LOG
final Logger LOG

few have

protected static final Logger log
final Logger log

this got me all confused as some have defined it as static final and protected where as some defined it as only final, few have declared the variable in all caps and some is normal form.

Can any one have idea what is the proper way for declaration or its a matter of personal choice or project specific decision?

4

4 回答 4

2

i always go for

private static final Logger logger.

The reason being that it should not be accessible to any class extending this class (as it would be with protected). The exteneding class should define it's own logger.

Using default (no protected/private/public) is even worse as it allows classes in the same package or subclasses to access the logger.

As for the comment about about using protected to avoid warnings - that is just lazy and bad coding. Warnings are there for a reason, they don't block compilation.

于 2012-05-22T09:53:18.987 回答
1

The best way is work without such a variable in every class, but use a static utility wrapper around slf4j from jcabi-log:

Logger.debug(this, "some variable = %s", value);
于 2012-10-05T06:34:15.893 回答
0

I also use

private static final Logger logger = Logger.getLogger(MyClass.class).

The only reason to use non-static declaration:

private final Logger logger = Logger.getLogger(getClass())

is that it can be safely copy/pasted from one class to another without mistakes. Very often you copy-paste or refactor your classes and then use Logger.getLogger(One.class) inside class Two that may be confusing when configuring logger for debugging.

于 2012-05-22T09:57:21.827 回答
0

If I understood you correctly, what confuses you is:

  1. the first example's variable name is uppercase: LOG.
  2. the second example's variable name is lowercase: log.
  3. In one case it's declared simply as a final type variable, whereas in the other it's declared as a protected static final type variable.

If so,

LOG and log are exactly the same. However, you should know it is common practice for programmers to declare their constants in uppercase. The final keyword in Java defines a variable which you want to remain constant. So again, it is not required by the compiler to write your constants in uppercase, but it is common practice to do so - that's why you see some programmers use lowercase and others uppercase; it is a matter of preference.

The protected keyword in Java means you want the variable to be accessible by not only the methods of the class, but any methods of any subclass that inherits from it.

The static keyword in Java, when applied to variables/fields, states that these fields can only be referenced by static methods.

So,

protected static final Logger LOG means LOG will be a constant variable and will not change, and it can be accessed by static methods of this class, which can in turn be accessed by objects of any class that inherits from this class.

And,

final Logger LOG means LOG will be a constant variable and will not change, and it can be accessed by ANY method in the PACKAGE - it does not matter if inheritance is present or not.

Like the others, I would go with private static final logger LOG as well.

Please read the following documentation on Java Access Controls for a deeper understanding of what's going on:

Access Controls in Java

Hope this helps you.

于 2012-05-22T10:31:29.797 回答