-1

I was working on some code in which I need to access the variable "hs" present in the static block of one class from another. Note: Both the class are preset in different packages.

Code is as follow:

public class A{
    static {
        HashSet<String> hs = new HashSet<>();
    }
}

I Googled about it but nothing found anything helpful. Your help would be very appreciable.

EDIT: I am not allowed to make changes in this file still need to access it from the other file.

Why I need to do this cause I am doing unit testing by JUnit and there is nothing what this block is returning which I can put assertEquals() on. So the option I left with is to test the side-effects and this variable "hs" value is getting changed as a side-effect. That's why I need to access it from another file.

  • @lealceldeiro Thanks for the quick reply. Actually the work is something like that I can't make changes in the code, and still need to access it from the another file. Can you help me with it. – user11498795 Aug 17 '19 at 10:07
  • 3
    There is no way for you to access this variable. It only exists temporary during the static block and has nothing to do with the class `A`. – second Aug 17 '19 at 10:10
  • 4
    This might be an [`XY Problem`](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Maybe you can add an explanation why you even wan't do that? – second Aug 17 '19 at 10:27
  • 1
    @lealceldeiro No it isn't. No access modifier is associated with a local variable. – user207421 Aug 17 '19 at 10:32
  • @second I have edited the explanation, check if it clarifies that why i need to do this. – user11498795 Aug 17 '19 at 10:36
  • In unit tests you are supposed to test `public methods` and `object states` not local variables. `hs` is not a `side-effect` unless it is assigned to another variable that has something to do with the class `A`. Maybe you want to add what else the static block is doing. – second Aug 17 '19 at 10:37
  • If you can't change this code your question embodies a contradiction in terms. And as you agree that 'there is nothing what this block is returning', there is nothing here that needs testing. – user207421 Aug 17 '19 at 10:37

2 Answers2

1

Declare it as public static inside the class and initialize it in static block

class A1{
public static HashSet<String> hs;
static {
     hs= new HashSet<>();
    }
 }
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
1

Need create getter and setter for variable "hs".

Class 1:

public class Test {

    public static HashSet<String> hs;

    static {
        hs = new HashSet<>();
        hs.add("Test14");
        hs.add("Test15");
        hs.add("Test16");
    }

    public static HashSet<String> getHs() {
        return hs;
    }

    public static void setHs(HashSet<String> hs) {
        Test.hs = hs;
    }


}

Class 2

If you need to use "hs" variable in without static method then:

public class Test2 {

    public void test() {
        Test ts = new Test();
        ts.getHs();
    }
}

If you need to use "hs" variable in with static method then:

public class Test2 {

    public static void test() {
        Test.getHs();
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483