0

I was wondering if anyone could tell me why I am getting an error on line 175

Student george= new Student("George Flintstone", 21, "Politics", 3.1);

It is saying there is no enclosing instance of type person accessible. I have looked through the other similar questions but I'm not very familiar with java so I'm not really finding an answer, at least one I understand well enough to. This is homework from my intro to java class. I didn't see anything in the FAQ saying I couldn't ask for help with homework, but ill admit I didn't read it very carefully, so if I'm breaking some rule or another just let me know.

Also, sorry for posting so much code, I was not sure what parts of it might be important.

Public class Person {

    String name;
    int age;

    public Person(String n, int a)
    {
        name = n;
        age = a;
    }

    public Person()
    {
        name = "Bob";
        age = 24; 
    }

    public void set_Person(String n)
    {
        name = n;
    }

    public void set_Person(int a)
    {
        age = a;
    }

    public void set_Person(String n, int a)
    {
        name = n;
        age = a;
    }

    public String get_Name()
    {
        return name;
    }

    public int get_Age()
    {
        return age;
    }

    public boolean equals(Object ob)
    {
        Person p = (Person)ob;
        if(ob == null)
            return false;
        if(name.equals(p.name) && age == p.age)
            return true;
        else
            return false;
    }

    public String toString()
    {
        String temp = name + " is " + age + " years old ";
        return temp;
    }

class Student {

    String major;
    double gpa;
    Person p = new Person();

    Student(String n, int a, String m, double g )
    {
        p.set_Person(n,a);
        major = m;
        gpa = g;
    }

    Student()
    {
        p.set_Person("Ben", 10);
        major = "compsci";
        gpa = 3.5;
    }

    public void set_Student(String n, int a, String m, double g)
    {
        p.set_Person(n,a);
        major = m;
        gpa = g;
    }

    public void set_Student(String n)
    {
        p.set_Person(n);
    }

    public void set_Student(int a)
    {
        p.set_Person(a);
    }

    public String getStudentName()
    {
        String temp = p.get_Name();
        return temp;
    }

    public int getStudentAge()
    {
        return p.get_Age();
    }


}

class Family {


    Person [] per_array;
    int person_count = 0;
    Person temp = new Person();

    Family(int members)
    {
        Person[] per_array = new Person[members];
    }

    public void addPerson(Person p)
    {
        boolean check = false;

        for(int count = 0; count < per_array.length; count++){
            if(per_array[count].equals(p))
            {
                System.out.println("That person is already a member of this family");
                check = true;
            }
        }
        if(check = false){
            per_array[person_count] = p;
            person_count++;
        }
    }

    public void addPerson(Student s)
    {
        temp.set_Person(s.getStudentName(), s.getStudentAge());
        boolean check = false;

        for(int count = 0; count < per_array.length; count++){
            if(per_array[count].equals(temp))
            {
                System.out.println("That person is already a member of this family");
                check = true;
            }
        }

        if(check = false){
            per_array[person_count] = temp;
            person_count++;
        }

    }

    public void printOutFamily()
    {
        for(int count = 0; count < per_array.length; count++)
        {
            per_array[count].toString();
        }
    }
}

    public static void main(String[] args) {
        Person fred= new Person("Fred Flintstone", 50);
        System.out.println("created " + fred);

        Person wilma = new Person("Wilma Flintstone", 48);
        Student george= new Student("George Flintstone", 21, "Politics", 3.1);
        System.out.println("created " + george);

        Student sue= new Student("Sue Flintstone", 24, "Nursing", 3.3);
        Student anotherGeorge= new Student("George Flintstone", 21, "Math", 3.4);
        Person yetAnotherGeorge= new Person("George Flintstone", 21);

        Family f = new Family(10);
        f.addPerson(fred);
        f.addPerson(wilma);
        f.addPerson(george);
        f.addPerson(sue);
        f.addPerson(anotherGeorge);
        f.addPerson(yetAnotherGeorge);

        anotherGeorge.set_Student("Georgie Flintstone");
        f.addPerson(anotherGeorge);

        f.printOutFamily();
    }

}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 1
    If `Person` and `Student` are in the same file, good chances are you have your curly braces misplaced. – Sergey Kalinichenko Apr 10 '13 at 02:17
  • Why doesn't `Student` just `extend` `Person` instead? – MadProgrammer Apr 10 '13 at 02:34
  • Possible duplicate of [No enclosing instance of type Server is accessible](http://stackoverflow.com/questions/7901941/no-enclosing-instance-of-type-server-is-accessible) – Raedwald Mar 02 '16 at 22:32
  • @Raedwald: This question here is the much better duplicate since it's much simpler and also minimal: http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible – fabian Mar 03 '16 at 22:47

1 Answers1

2

Student is an inner class which is associated with a particular instance of Person which means that it is invalid to call

new Student("George Flintstone", 21, "Math", 3.4);*

Making the inner class static will allow it to work. A static inner classes, aka a nested class, doesn't need an instance of the outer class, Person here.

Reimeus
  • 158,255
  • 15
  • 216
  • 276