noMAD
noMAD

Reputation: 7844

Constructor and objects of static inner class

public class SomeClass {
  //Some code
  private static InnerClass {
    String test;
    private InnerClass(String test) {
      this.test = test;
    }

    // Using test here in some way
    test.split("something"); //Compiler error, test might not have been initialized
}

Why does the compiler complain about that? I am initializing test in the constructor. If the compiler is complaining, that means there might be a way to access test without going through the constructor. I tried that, but no luck without reflection. What am I missing here?

Upvotes: 0

Views: 67

Answers (2)

Chamly Idunil
Chamly Idunil

Reputation: 1872

write new method and move this operation into it.

ex :

private void splitTest() {
test.split("something");
}

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240966

statement

 test.split("something");

should be in executable block (method/ constructor/ static initilization blocks)

Upvotes: 6

Related Questions