0

Trying to troubleshoot errors in the psvm, I honestly have no clue what's wrong or what to google pertaining to this. It wont let me post this without more words but I honeslty don't know whats wrong with the program to give any more details.

public class backToSchoolDalton{
        public class Person{
            //Person Code
        public class Teacher extends Person{
            //TeacherCode
        public class Student extends Person{
            //StudentCode
        public class CollegeStudent extends Student {
            //CollegeStudentCode
        public static void main(String[] args) {

        ERROR HEAVEN RIGHT HERE

        backToSchoolDalton a = new backToSchoolDalton();
        backToSchoolDalton b = new backToSchoolDalton();
        backToSchoolDalton c = new backToSchoolDalton();
        backToSchoolDalton d = new backToSchoolDalton();
        a.Person("Coach Bob", 27, "M");
        b.Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
        c.Teacher("Duke Java", 34, "M", "Computer Science", 50000);
        d.CollegeStudent("Ima Frosh", 18, "F", "UCB123",
                4.0, 1, "English");

        Person bob = new Person("Coach Bob", 27, "M");
        System.out.println(bob);

        Student lynne = new Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
        System.out.println(lynne);

        Teacher mrJava = new Teacher("Duke Java", 34, "M", "Computer Science", 50000);
        System.out.println(mrJava);

        CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
                4.0, 1, "English");
        System.out.println(ima);
    }
}

Please help!

sinθ
  • 11,093
  • 25
  • 85
  • 121
  • 1
    Can we do that in java what you have posted in your code? – Vinay Prajapati Jan 05 '16 at 04:10
  • 3
    you really need to take this question down and read a Java language tutorial...your code doesn't make much sense – Leo Jan 05 '16 at 04:11
  • 1
    Why are you adding so many inner classes? Your a,b,c,d instances doesn't make sense! Even if you can make it compliable, it is a bad piece of code :( – user3437460 Jan 05 '16 at 06:30

3 Answers3

0
public static void main(String[] args) {
    backToSchoolDalton s = new backToSchoolDalton();

    Person bob = s.new Person("Coach Bob", 27, "M");
    System.out.println(bob);

    Student lynne = s.new Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
    System.out.println(lynne);

    Teacher mrJava = s.new Teacher("Duke Java", 34, "M", "Computer Science", 50000);
    System.out.println(mrJava);

    CollegeStudent ima = s.new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
            4.0, 1, "English");
    System.out.println(ima);
}
0

There are two (common) types of nested classes. Inner classes and static nested classes. An inner class is coupled with an instance of its surrounding class, meaning you need to construct an instance of the surrounding class before you can use it. A static nested class, on the other hand, is not. It's just like any other class, except it's defined inside another class for organizational reasons.

The solution you posted, to construct an instance of backToSchoolDalton, works because you now have an instance of the surrounding class. But that isn't actually what you want. Instead, simply make your inner classes static nested classes and you can construct these classes directly.

public static class Person {
  //Person Code
}

public static class Teacher extends Person {
  //TeacherCode
}

...

Teacher c = new Teacher("Duke Java", 34, "M", "Computer Science", 50000);
Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
-2

Edit: My below answer is wrong. You can create instances of a class inside the same class as long as that class isn't doing so in the constructor. Not deleting my answer so others can learn from my mistake

As far as I know, you can't declare instances of a class inside the same class. You need to move your main() outside of backtoSchoolDalton():

backToSchoolDalton() {
    //dalton code
}

public static void main(String[] args) {

    backToSchoolDalton a = new backToSchoolDalton();
    backToSchoolDalton b = new backToSchoolDalton();
    backToSchoolDalton c = new backToSchoolDalton();
    backToSchoolDalton d = new backToSchoolDalton();
    a.Person("Coach Bob", 27, "M");
    b.Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
    c.Teacher("Duke Java", 34, "M", "Computer Science", 50000);
    d.CollegeStudent("Ima Frosh", 18, "F", "UCB123",
        4.0, 1, "English");

    Person bob = new Person("Coach Bob", 27, "M");
    System.out.println(bob);

    Student lynne = new Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
    System.out.println(lynne);

    Teacher mrJava = new Teacher("Duke Java", 34, "M", "Computer Science",
    50000);
    System.out.println(mrJava);

    CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
        4.0, 1, "English");
    System.out.println(ima);

}
Ethan Fischer
  • 1,229
  • 2
  • 18
  • 30
  • It gives an error if the main is outside of the class sharing the file name. – Mine Raid Jan 05 '16 at 04:31
  • Oh try putting backToSchoolDalton in your main function – Ethan Fischer Jan 05 '16 at 04:34
  • That's not even possible – Mine Raid Jan 05 '16 at 04:37
  • Okay, you need to declare your Dalton class in a separate file. But still create instances of it in your main function in this file. – Ethan Fischer Jan 05 '16 at 04:38
  • You certainly can nest classes inside other classes. – dimo414 Jan 05 '16 at 06:19
  • Yes. But you were declaring instances of backToSchoolDalton inside your backToSchoolDalton class. This creates an infinite loop and probably was giving you the error. – Ethan Fischer Jan 05 '16 at 06:30
  • This answer, and all your comments, are gibberish. You don't create infinite loops by defining inner classes, nor by constructing instances of them inside the `main()` method. – dimo414 Jan 05 '16 at 06:35
  • You're right, neither of those will create an infinite loop. However, creating an instance of a class in that SAME class will. In your definition of backToSchoolDalton, you tried creating 4 backToSchoolDalton objects, thereby invoking backToSchoolDalton 4 more times, each of which would create 4 more backToSchoolDalton objects... and so on. Sorry if this was unclear – Ethan Fischer Jan 05 '16 at 07:06
  • 1
    @Ethan Fischer No, only instantiating them during class-instantiation will cause an infinite loop. How would `clone()` be possible otherwise? – piet.t Jan 05 '16 at 07:17
  • @piet.t Isn't that happening in Mine Raid's code? As soon as he/she creates an instance of backToSchoolDalton, it will make 4 more instances of backToSchoolDalton, and those 4 will each make 4 more... – Ethan Fischer Jan 05 '16 at 07:34
  • 1
    @Ethan Fischer The only place I see instantiation of `backToSchoolDalton` is in `main()` and that method is not called upon object-creation. – piet.t Jan 05 '16 at 07:47
  • Ahh. You're right. For some reason I thought main() would be called whenever backToSchoolDalton is instantiated. Sorry everybody :( – Ethan Fischer Jan 05 '16 at 07:57
  • 1
    `As far as I know, you can't declare instances of a class inside the same class` this sentence is not true. Well, I **didn't** down vote you, but just to let you know that isn't true. – user3437460 Jan 05 '16 at 08:26
  • @user3437460 Yeah, this has been a real learning experience for me ;) – Ethan Fischer Jan 05 '16 at 08:55