How do static variables work in Junit when initiailizing them from multiple modules?

Viewed 2461

Lets assume I have a class with a static variable like so:

public class var{
    public static String myvar = "thor";
}

And junit classes like so:

public class test1{
    @Test
    public void test(){
       var.myvar = "1";
    }
}

public class test2{
     @Test
    public void test(){
       var.myvar = "1";
    }
}

When running both junit classes, will they share the same variable? or somehow junit makes these variables thread safe so each module has its own static variable?

update: if they case is the former how can you set up a static variable for the entire junit build?

1 Answers

A static variable is linked with the class , so since there is only one .class instance , the variable myvar will also be one and wherever you access it, it will the same varaible.

Related