Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: how to "restart" a static class?

Tags:

java

junit

static

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

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
    }

}

See Foo.java:

public class Foo {

    public static int i = 0;

}

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

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


EDIT

ALMOST SOLUTION:

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("\ntest()");

        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.");
    }


}

OUTPUT

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();

I get error:

java.lang.ClassCastException
like image 311
Topera Avatar asked Jan 08 '11 00:01

Topera


People also ask

Can we reinitialize static variable?

Declaring variables only as static can lead to change in their values by one or more instances of a class in which it is declared. Declaring them as static final will help you to create a CONSTANT. Only one copy of variable exists which can't be reinitialize.

Can you return a static method?

it is a static method (static), it does not return any result (return type is void), and. it has a parameter of type array of strings (see Unit 7).

Can static class be changed?

It's like a global variable that every instance of the class can access. But a static variable is not constant because it can be changed at any time.


2 Answers

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)

like image 70
StaxMan Avatar answered Sep 27 '22 22:09

StaxMan


Create a static method that sets the class variables to their initial values, then call it when you need it.

like image 43
martin clayton Avatar answered Sep 27 '22 21:09

martin clayton