Hi,

I am following the Java Tutorial for Lamda Expressions Approach 3 Specify Search Criteria Code in a Local Class at https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#approach3.
I am using JDK8 and have a compile error as follows:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    No enclosing instance of type Person is accessible. Must qualify the 
    allocation with an enclosing instance of type Person (e.g. x.new A() where x is an instance of Person).

    at java_testing/testing.Person.main(Person.java:65  which is copied from the tutorial.
    
    Line 65 printPersons(roster, new CheckPersonEligibleForSelectiveService());

If I add any instance of Person, it works and prints the eligible Person objects from the List.

e.g. printPersons(roster, person3.new CheckPersonEligibleForSelectiveService());
I suspect the error is in main() because that is what I supplied, but cannot figure out why it works
with any instance of Person.

At Line 65 in main, I tried printPersons(roster, new CheckPersonEligibleForSelectiveService());

I expected all eligible Person objects from the list that meet the criteria to print out, but received a compile error:

Must qualify the 
    allocation with an enclosing instance of type Person (e.g. x.new A() where x is an instance of Person).

If I add any instance of a Person object from the list, it succeeds by printing all the eligible Person objects.

printPersons(roster, new person0.CheckPersonEligibleForSelectiveService(); //Succeeds, but seems arbitrary because any instance of Person succeeds.

    The code is:

package testing;
import java.util.List;
import java.util.ArrayList;
import java.util.*;

class Person {
    
    public enum Sex {
        MALE, FEMALE
    }

    String name;
    int age;
    Sex gender;
    
    public Person(String name, int age, Sex gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }
    
    int getAge() {
        return this.age;
    }
    
    String getName() {
        return this.name;
    }
        
     public void printPerson(Person p) {
         System.out.print(p.name + '\n');
     }
    
     public static void printPersons(
        List<Person> roster, CheckPerson tester) {
         for (Person p : roster) {
            if (tester.test(p)) {
                p.printPerson(p);
            }
        }
     }

     interface CheckPerson {
        boolean test(Person p);
     }

     class CheckPersonEligibleForSelectiveService implements CheckPerson {
        public boolean test(Person p) {
            return p.gender == Person.Sex.MALE &&
                p.getAge() >= 18 &&
                p.getAge() <= 25;
        }
     }
     
     public static void main(String[] args) {
            Person person0 = new Person("Bob", 23, Person.Sex.MALE);
            Person person1 = new Person("Joe", 20, Person.Sex.MALE);
            Person person2 = new Person("Alice", 22, Person.Sex.FEMALE);
            Person person3 = new Person("Mel", 23, Person.Sex.MALE);
            List<Person> roster = new ArrayList<Person>();
            roster.add(person0);
            roster.add(person1);
            roster.add(person2);
            roster.add(person3);
            printPersons(roster, new CheckPersonEligibleForSelectiveService());
//          printPersons(roster, new person0.CheckPersonEligibleForSelectiveService(); This works,
// but I do not know why it works with any instance of Person.
        }
}
0

There are 0 answers