开发者

Java derived class not automatically creating instance

I have a base abstract logger class, that has an instance variable that I want to be set by the derived class automatically when the code is create. So here is the base class:

abstract public class CLog 
{
    /** Maintains the call stack level for each thread */
    private static HashMap<Integer, Integer> callStackLevel = new HashMap<Integer, Integer>();

    /** Static instance to be set by the derived class */
    private static CLog instance = null;

    /** Logs in verbose */
    public static void v(String message) { if(instance != null) instance.verbose(getMessage(message)); }
    /** Logs in debug */
    public static void d(String message) { if(instance != null) instance.debug(getMessage(message)); }
    /** Logs in informational */
    public static void i(String message) { if(instance != null) instance.info(getMessage(message)); }
    /** Logs in warning */
    public static void w(String message) { if(instance != null) instance.warn(getMessage(message)); }
    /** Logs in error */
    public static void e(String message) { if(instance != null) instance.error(getMessage(message)); }

    /**
     * Calculates the message (with header)
     */
    private static String getMessage(String message)
    {
        ...
    }

    /** Constructor sets instance */
    protected CLog() { instance = this; }

    /** Logs in verbose */
    protected abstract void verbose(String message);
    /** Logs in debug */
    protected abstract void debug(String message);
    /** Logs in informational */
    protected abstract void info(String message);
    /** Logs in warning */
    protected abstract void warn(String message);
    /** Logs in error */
    protected abstract void error(String message);
}

I creates the derived class for an android logger. I want it to automatically call the constructor, but it seems this isn't working because nothing is resulting from all my logging functions.

public class AndroidLog extends CLog 
{
    protected static AndroidLog derived = new AndroidLog();

    @Override
    protected void debug(String message) {
        Log.d("Crystal", message);
    }

    @Override
    protected void error(String message) {
        Log.e("Crystal", message);
    }

    @Override
    protected void info(String message) {
        Log.i("Crystal", message);
    }

    @Override
    protected void verbose(String message) {
        Log.v("Crystal", message);
    }

    @Override
    protected void warn(String message) {
        Log.w("Crystal", message);
    }
}

Why is this not working? When I call the static function in the base class, I am not getting any logs.

开发者_运维技巧Is there anyway by just editting the AndroidLog class or by making an edit in the CLog class that isn't dependent on AndroidLog to make this work?


The mere existence of the AndroidLog class won't cause it to bootstrap itself!

In Java classes are loaded and initialized when they are used, not before! So unless any class of yours somewhere references AndroidLog in some relevant way, it will never be loaded, it's static fields never initialized and its constructor will never be called.


Another important aspect is the fact that private static class members are not inherited! Your AndroidLog class will not have access to any of the private static members in the base class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜