0

I am trying to create a new object of teacher and student but it is giving me an error. It stops giving me the error if I remove static from main but then it can't find main to run. I really do not understand why it is giving me this error. Any help is appreciated.

public class Person
{
   String personName = "";
   String personAddress = "";

   public Person(String name, String address)
   {
      personName = name;
      personAddress = address;
   }

   class Student 
   {
      int numCourses = 0;
      String[] courses = new String[10];
      int[] grades = new int[10];
      
      Student(String name, String address)
      {
         personName = name;
         personAddress = address;
      }
      
   }
   
   class Teacher 
   {
      int numCourses = 0;
      String[] courses = new String[20];
      
      public Teacher(String name, String address)
      {
         personName = name;
         personAddress = address;
      }
      
   }
   
   public static void main(String[] args)
   {
      Student student1 = new Student("Aaron", "1234 this road S"); //error pointing at new

      Teacher teacher1 = new Teacher("Fatima", "1111 some road N"); //error pointing at new
   }
}

1 Answers1

0

You've got a couple things going on here, so I'll start at the beginning, although this might be show your question to be "too broad" for this site. Your code looks like C#, so I'll use that language for example code, but it should work similarly in Java and other similar OO languages.

First of all, main needs to be static for the compiler to pick it up and run the program, and so does the class it resides in. Let's move that around some.

internal static class Program
{
   public static void Main(string[] args)
   {
      Student student1 = new Student("Aaron", "1234 this road S");

      Teacher teacher1 = new Teacher("Fatima", "1111 some road N");
   }
}

I gave this class a generic name, since we need the "Person" class name or the next bit.

Normally, you want to have classes in their own files. This is a code styling thing. It's not a hard rule, but it's a very, extremely, strong recommendation among the places I've worked (and I have 10 years professional experience). So lets create a new file.*

public class Person
{
   string personName;
   string personAddress;

   public Person(String name, String address)
   {
      personName = name;
      personAddress = address;
   }
}

Now we don't need inheritance, but it makes things easier. So here's a couple new files.

class Student : Person
{
   int numCourses = 0;
   string[] courses = new String[10];
   int[] grades = new int[10];

   public Student (string name, string address): base (name, address)
   {}
}

and

class Teacher : Person
{
   int numCourses = 0;
   string[] courses = new String[20];

   public Teacher(string name, string address): base (name, address)
   {}
}

And just like "magic", it works. So why?

Well, like I said, the compiler needs the Main method to run the program, so that has to be static. And, so the compiler knows where to at least attempt to look for Main, the class containing it needs to be static, too.

Static methods can't call non-static methods or variables directly. You have to create an instance of the method in order to call the method. That is done by creating a new, non-static class that contains the non-static method.
How do I call a non-static method from a static method in C#?

This article describes it a little bit, but it's generally frowned on to create an instance of a class within that instance of the class.

With inheritance, you can create one class like you did with Person, then use it as a basis for other classes, like you were trying with Student and Teacher, you just had it inside out, sort of. With using the base keyword in your individual constructors, you are calling the base constructor with the needed variables.

Another style guide is that you don't need to assign a default value to a variable or property when you are going to assign a real value to it in your constructor.

*Yes, this small of a program will work with it all being in one file, but when/if this grows larger, it'll become unmanageable. Also, your (future) co-workers may/will dislike you for cramming everything into a single file. Coding styles change over time and by team, so YMMV.

computercarguy
  • 2,173
  • 1
  • 13
  • 27