Java:如何“重启"静态类? [英] Java: how to "restart" a static class?

查看:23
本文介绍了Java:如何“重启"静态类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态类 (Foo) 和一个主类 (Main)

I have a static class (Foo) and a main class (Main)

见 Main.java:

See Main.java:

public class Main {

    public static void main(String[] args) {
        System.out.println(Foo.i); // 0
        Foo.i++;
        System.out.println(Foo.i); // 1
        // restart Foo here 
        System.out.println(Foo.i); // 1 again...I need 0
    }

}

参见 Foo.java:

See Foo.java:

public class Foo {

    public static int i = 0;

}

有没有办法重新启动或重置静态类?

Is there any way to restart or reset a static class?

注意:我需要这个,因为我正在用 jUnit 测试一个静态类,我需要在第二次测试之前清理参数.

Note: I need this because I'm testing a static class with jUnit and I need to clean parameters before second test.

编辑

几乎解决方案:

使用 StanMax 回答,我可以:

Using StanMax answer, I can to this:

Main.java

public class Main {

    public static void main(String[] args) throws Exception {
        test();
        test();
    }

    public static void test() throws Exception {

        System.out.println("
test()");

        MyClassLoader myClassLoader = new MyClassLoader();
        Class<?> fooClass = myClassLoader.loadClass(Foo.class.getCanonicalName());

        Object foo = fooClass.newInstance();
        System.out.println("Checking classloader: " + foo.getClass().getClassLoader());

        System.out.println("GC called!");
        System.gc();
    }

}

MyClassLoader.java

public class MyClassLoader {

    private URLClassLoader urlClassLoader;

    public MyClassLoader() {
        try {
            URL url = new File(System.getProperty("user.dir") + "/bin/").toURL();
            URL[] urlArray = {url};  
            urlClassLoader = new URLClassLoader(urlArray, null);  
        } catch (Exception e) {
        }
    }

    public Class<?> loadClass(String name) {
          try {
            return (Class<?>) urlClassLoader.loadClass(name);
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("MyClassLoader - End.");     
    }


}

Foo.java

public class Foo {

    public static int i = 0;

    static {
        System.out.println("Foo - BEGIN ---------------------------------");
    }

    public void finalize() throws Throwable {
        System.out.println("Foo - End.");
    }


}

输出

test()
Foo - BEGIN ---------------------------------
Checking classloader: java.net.URLClassLoader@ec160c9
GC called!
MyClassLoader - End.
Foo - End.

test()
Foo - BEGIN ---------------------------------
Checking classloader: java.net.URLClassLoader@ec3fb9b
GC called!
MyClassLoader - End.
Foo - End.

问题:如果我做下面的演员表:

PROBLEM: if I do the cast bellow:

Foo foo = (Foo) fooClass.newInstance();

出现错误:

java.lang.ClassCastException

推荐答案

只有当您可以卸载类时,才能重新加载它,因为类加载时会执行类静态代码.

Only if you can unload class, get it re-loaded, as class static code gets executed when class is loaded.

但是你可以直接修改值:

But you can just directly modify the value:

Foo.i = 0;

(或创建等效的方法来执行此操作,尤其是如果静态成员不是公共的)

(or create equivalent method for doing it, esp. if static member is not public)

这篇关于Java:如何“重启"静态类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆