0

I'm experiencing the following warning without a clue how to handle it. As you can see I have a [Course] Class, with generic property [idOrName]. when I try to create the inseretion method I get the following worning:

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

where PR3 is the class name.

following is the code, the warning is at the line set.add(new Course(name,avg));

 private static void insertCoursesToSet(Set<Course> set, int n, int it) {
        Scanner s = new Scanner(System.in);

        if(it==1) 
            for(int i=0 ; i<n ; i++){
                System.out.println("Please enter name:");
                s.nextLine(); //clear buffer
                String name = s.nextLine();
                System.out.println("Please enter avg:");
                float avg = s.nextFloat();
                set.add(new Course<String>(name,avg));
            }
        else
            for(int i=0 ; i<n ; i++){
                System.out.println("Please enter id:");
                Integer id = s.nextInt();
                System.out.println("Please enter avg:");
                float avg = s.nextFloat();
                set.add(new Course<Integer>(id,avg));
            }
    }

    public class Course<E>{
        private E idOrName;
        private float avg;

        public Course(E idOrName, float avg){
            this.idOrName = idOrName;
            this.avg = avg;
        }
    }
Gil Peretz
  • 2,399
  • 6
  • 28
  • 44
  • Did you create separate file with class `Course`? – Maxim Shoustin Dec 21 '13 at 13:09
  • The indentation of your curly brackets barely makes any sense... Could you please fix that, I am wary of suggesting an edit myself, because you might have intended it differently as it seems. – skiwi Dec 21 '13 at 13:13
  • 1
    See this answer for more information on what the error exactly is: http://stackoverflow.com/questions/9744639/must-qualify-the-allocation-with-an-enclosing-instance-of-type-geolocation?rq=1 – skiwi Dec 21 '13 at 13:14

1 Answers1

2

It happens because you created inner class Course into PR3 and insertCoursesToSet method is static.

If you interesting to leave it as inner class, make it static also:

public static class Course<E>{
    private E idOrName;
    private float avg;

    public Course(E idOrName, float avg){
        this.idOrName = idOrName;
        this.avg = avg;
    }
}

OR

Make insertCoursesToSet non-static and you can write:

private void insertCoursesToSet(Set<Course> set, int n, int it) {
    Scanner s = new Scanner(System.in);

    if(it==1) 
        for(int i=0 ; i<n ; i++){
            System.out.println("Please enter name:");
            s.nextLine(); //clear buffer
            String name = s.nextLine();
            System.out.println("Please enter avg:");
            float avg = s.nextFloat();
            set.add(new Course<String>(name,avg));
        }
    else
        for(int i=0 ; i<n ; i++){
            System.out.println("Please enter id:");
            Integer id = s.nextInt();
            System.out.println("Please enter avg:");
            float avg = s.nextFloat();
            set.add(new Course<Integer>(id,avg));
        }
}

public  class Course<E>{
    private E idOrName;
    private float avg;

    public Course(E idOrName, float avg){
        this.idOrName = idOrName;
        this.avg = avg;
    }
}
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225