title: How can I access private field in enclosing class when creating instance with overridden method in static factory method?
body: <p>I'm not sure what is going on here. The line causing the error is inside an implementation of an object of type Printer which has a field named 'string'. The whole thing is contained inside the abstract class named 'Printer'. How can I achieve what I want while keeping the 'string'-field private?</p>
  
  <pre><code>public abstract class Printer {
  
     static Printer blahPrinter(){
        Printer blahPrinter =  new Printer("blah") {
           @Override
           void printString() {
              System.out.println(this.string); //Here is the error: "string has private access in Printer"
           }
        };
        System.out.println(blahPrinter.string); //No error on this line
        return blahPrinter;
    }
  
    private final String string;  //Compiles and works as expected if I use a more visible access modifier
  
    public Printer(String string) {
       this.string = "I say " + string;
    }
  
    abstract void printString();
  
    public static void main(String[] args) {
       final Printer blahPrinter = Printer.blahPrinter();
       blahPrinter.printString();
    }
  
  }
  </code></pre>