1

In Java is it better to have an object, such as a logger, declared as

private static final Logger logger = LoggerFactory.getLogger();

or

 private static final Logger LOGGER = LoggerFactory.getLogger();

I think that the first option is better, becuase when you call methods on it such as

logger.info("Some information...");

it looks better and is more readable, but I'm not sure what best practice is?

The other option is to declare the object as

private final Logger logger = LoggerFactory.getLogger();

but I thought that in Java if you can declare something as static then you should.

LockieR
  • 370
  • 6
  • 19

3 Answers3

1

Where I work it is convention to capitalise these objects, but because they're constants (final) rather than because they're static. I would also declare it as static in this case because the logger shouldn't be dependant on an instance of the class but the class itself.

Robb
  • 139
  • 4
1

Generally constants are declared in uppercase letters. Again it depends on project preference. This may be duplicate of thread Java naming convention for static final variables.

1

According to the java conventions, anything that is declared as static final should be in the capital so your first case is according to the convention, and for the last case, static variable follow normal variables convention.