non-static method cannot be referenced from a static context


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: non-static method cannot be referenced from a static context

  1. #1
    Join Date
    Mar 2004
    Posts
    20

    Exclamation non-static method cannot be referenced from a static context

    hi, i've basically been doin my assignement, we have to create a very simple student registeration thing for a college, i've just implemented basic stuff to get the functionality goin, i created my Student Class, which has two sub-classes FullTimeStudent and PartTimeStudent.

    now i'm creating a register class, this class should be able to store "Student" objects in an array, and have methods to access the array and manipulate the contents.

    from this class we have been guided to have ATLEAST the following:

    - add a new student of either FullTimeStudent or PartTimeStudent
    - remove a student with a given student id number...
    - request fees due for a given student...
    and stuff on similar terms to that.

    my problem lies with the first one.

    from what i gathered, the lecturer was hinting at creating a single method from which i can create a student object and make either a FullTime or PartTime student.

    i didn't know exactly how to do this, as i will be implenting a simple interface later, i decided that i will create two seperate methods so i can jut call the appropriate method, one to create a FullTimeStudent and for PartTime (using the appropriate classes constructor. When i tried this, and i tried to test if it worked, i got the static method error thingy in the subject line!

    please find attached my java code files zipped!

    any help and soon will be very well appreaciated!!!
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Hope this can be used.

    I have created a little system, based on your source
    with some options:
    0 : exit
    1 : add fulltime student
    2 : add parttime student
    3 : remove a student
    4 : get fees due for a student
    5 : List fulltime students
    6 : List parttime students
    7 : List all students

    I've also made Student an abstract class and moved a couple of your
    methods around. After all, the system never deals w. Student, - it's either
    a Fulltime or a PartTime student. I've followed your lecturers hint, and
    some of my own too...

    Good luck !

    PS: you may want to remove the package directive on top of the sorce files.
    Attached Files Attached Files
    Last edited by sjalle; 02-27-2005 at 08:44 AM.
    eschew obfuscation

  3. #3
    Join Date
    Mar 2004
    Posts
    20

    Talking

    Thank you so much, i appreaciate you takin a look and sortin it all out, the thing is, i don't understand some things, from the brief look i've had so far, what does making the Student class and Abstract class do?

    secondly, the abstract methods that are in the Student class, the ones that have nuthing in them... are they just there so that they can be implemented in the Sub-classes?

    Also in the sub-classes FullTime and PartTime Student, the registerStudent classes... apart from checking wether the student ID is already in use... what else is goin on there? (namely, the whole reg.get... thing)

    Finally, the Register class... wow! we've actually been asked to create one more class, a Register Interface class that deals with all the user input, i see you have included that all in the Register class... lol at the array comment! i will tell HER to go fly a kite, i hope she understands... her english isn't so gud!

    More on Register class, getStudent... the boolean tell?

    there's some more advanced stuff in there as well that i haven't done.

    Again, thanx for the program, it looks sweet, i will try to learn what i can from it and try and implement it my way into my coursework! i'm pretty sure i'll need more help later!

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Ok

    First of all: If you make an abstract class, you cannot use it in a newstateent, you can only use it for making new class extension. An
    abstract class must have at least one abstract method. These are
    empty in the abstract class, but the classes that extend the abstract class
    must implement them.

    The register is an arrayList. ArrayLists can store any kind of object. We
    store both types of students into it. Since they are both descendants of
    Student we can get them like:
    Student aStudent=(Student)studentRegister.get(i);
    So, if the method getFeesDue() was defined as a normal method
    in the Student class then this:
    totalDue += aStudent.getFeesDue();
    would invoke the Student class method, and that is wrong since this
    calculation is different for the two student types. We would have to to
    stuff like
    Code:
    Student aStudent=(Student)studentRegister.get(i);
    if (aStudent instanceof FullTimeStudent) {
      FullTimeStudent fs=(FullTimeStudent)aStudent;
      totalDue += fs.getFeesDue();
    } else if (aStudent instanceof PartTimeStudent) {
      PartTimeStudent ps=(PartTimeStudent) aStudent;
      totalDue += ps.getFeesDue();
    }
    If there was, say 5 different types of students, all with their own rules for
    calculating stuff then I hope you see the picture (messy).
    Using the abstract approach we can just write:

    Code:
    Student aStudent=(Student)studentRegister.get(i);
    totalDue += aStudent.getFeesDue();
    In other words; even though the 'Student' we are operating with here is
    somewhat obscure (what kind of student is it ?) we don't care. Java will invoke
    the FullTimeStudent class method getFeesDue() if this Student is a
    FullTimeStudent and the PartTimeStudent getFeesDue() if the Student is a
    PartTimeStudent.

    The method:
    public void registerStudent (Register reg)
    that is implemented in the two Student extensions takes care of the whole
    student initialization. I have added an empty constructor to the classes,
    I use that, and then I call upon the student object to register itself.
    I pass the Register pointer to the student object in this method so it can utilize the
    Register public functions, these are;

    getIntegerEntry(String type)

    getStudent(int studentID, boolean tell)

    and

    addStudent(Student aStudent)

    If you check the method you will see that it sets the values of the object.

    The register pointer is named reg in the registerStudent
    methods parameter list, so the method
    'reaches into' the Register object and uses Register methods by prefixing the method w.
    the pointer, like

    reg.getIntegerEntry("student id")

    I've included a boolean parameter in the getStudent method. The reason is that I want to use the method for retrieving a student and for checking
    a students existence. The 'tell' parameter tells the method to 'shut up' if set
    to false, if true, it prints the 'not found' message for non existing students.
    I will always know cause a nonexisting student search yields a null value.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center