mercredi 8 février 2023

How to define java.util.logging.Logger for non-static inner class

Dug stackoverflow + net but found no explicit examples of how to mitigate the following issue:

import java.util.logging.Logger;

public class MyClass {

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

    private class MyInner {
        private static final Logger logger = Logger.getLogger(MyInner.class.getName());
    }
}

The construct above is obviously not allowed: non-static inner cannot have static fields unless constant expression.

But what's best practice? A) Use logger of outer class instead? B) Use non-static inner logger? (by nature of inner classes, they are much more populous than outer, so that may be some issue) C) Define a sibling or even child static class LoggerForMyInnerClass? D) other?

I'd appreciate any tutorial articles, videos or whatever where "best practice" is explained. Thanks!

Aucun commentaire:

Enregistrer un commentaire