0

I have a class called Scraper with a static inner class Builder, and also a non-static inner class called StringPair, I'm trying to get one of the builder methods to construct a new StringPair(a,b) and add it to an ArrayList<StringPair>, however it doesn't compile, the compiler says Error: non-static variable this cannot be referenced from a static context and specifically highlights the new StringPair(a,b) as the source of the problem.

I don't understand what's wrong here, any help would be appreciated!

class Scraper {

    private final ArrayList<StringPair> stringPairs;

    Scraper(Builder builder) {

        stringPairs = builder.builderStringPairs;


    }

    public static class Builder {

        private static ArrayList<StringPair> builderStringPairs = new ArrayList<StringPair>();

        public Builder addStringPairs(String a, String b) {

            builderStringPairs.add(new StringPair(a, b));
            return this;
        }

        public Scraper build() {
            return new Scraper(this);
        }

    }

    class StringPair {
        String a,b;

        StringPair(String a, String b) {
            this.a = a;
            this.b = b;

        }



        String getA() {
            return a;
        }
        String getB() {
            return b;
        }


    }

}
0

1 Answer 1

5

Builder isn't an inner class - it's just a nested class, with no implicit reference to an instance of the containing class (Scraper).

StringPair, however, is an inner class. In order to construct an instance of StringPair, you need to have a reference to an instance of Scraper. So, options for making Builder work:

  • Make it an inner class (non-static)
  • Make StringPair a nested class (static)
  • Give Builder an instance somewhere, at which point you could use:

    builderStringPairs.add(instance.new StringPair(a, b));
    

I suspect the second option is the most appropriate one here - I can't see any reason why StringPair needs a reference to an instance of Scraper.

See section 8.1.3 of the JLS for more information about nested and inner classes.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I found the solution from your response. Actually I needed Builder to be static, since it's a builder, and I needed StringPair to be non-static, since I need to created instances of it for the ArrayList. So the problem was the static inner class cannot "see" the non-static inner class, so I just took out StringPair and made it a separate top-level class, and now it works.
@Sindikalis: You've misunderstood, and it's really important that you go back and look at this again to stop it being a problem next time. There's no need for StringPair to be non-static: "since I need to created instances of it for the ArrayList" does not suggest that it needs to be an inner class. You can still create instances of static nested classes - you're creating instances of Builder, after all. It just changes whether there's an implicit reference to an instance of the containing class.
Ok thanks I really appreciate your help! I guess I need to look into this a bit more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.