Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8709185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:18:23+00:00 2026-06-13T04:18:23+00:00

I have a class called Scraper with a static inner class Builder , and

  • 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;
        }


    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:18:25+00:00Added an answer on June 13, 2026 at 4:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Property which has nothing but get -methods. All the
I have a class called XMLtoXML.java and this is one of it's methods... import
I have one class called Global and two other activities. In each activity I
I have a class called Path for which there are defined about 10 methods,
I have a class called RemoteError with a self.fatal method on it. This methods
I have a class called Employee with its 2 private variables and also contains
I have a class called LocalConstants.... public static class LocalConstants { public static string
I have class called Chicken and in Chicken I have some methods, so in
I have a class called photo. This class has 4 fields, one of while
I have this class called Table: class Table { public string Name { get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.