0

Here's my code: The code should return the list of students with the relevant data from the arraylist. But the error is telling me it can't make a static reference to a non static method??

I've tried to make the methods static and it gives me another error.

//main function code
    String forename = null;
    String surname = null;
    String grade = null;
    String yesOrNo;
    double mark;
    int selection;

ArrayList<StudentClass> studentDetails = new ArrayList<StudentClass>();

switch(selection){
case 1: {
    if (studentDetails.isEmpty()){
        System.out.println("No Students Have Been Entered Yet");
        main(null);
        break;
                }
    else{
         for(int i = 0; i < studentDetails.size(); i++){
             StudentClass = studentDetails.get(i);
             System.out.println( StudentClass.getForename() + " " +
             StudentClass.getSurname() + " received a " + StudentClass.getGrade() +
             " for their Student Mark of " + StudentClass.getMark() + "." );
            }
        }
break;
\\ Error:Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
StudentClass cannot be resolved to a variable
Cannot make a static reference to the non-static method getForename() from the   type StudentClass
Cannot make a static reference to the non-static method getSurname() from the type StudentClass
Cannot make a static reference to the non-static method getGrade() from the type StudentClass
Cannot make a static reference to the non-static method getMark() from the type StudentClass

at students.main(students.java:60)
\\code for Class
public class  StudentClass {
      public String Forename;
      public String Surname;
      public  String Grade;
       public  double Mark;

public StudentClass(String forename, String surname, double mark){

       Forename = forename;
   Surname = surname;
   Mark = mark;
   }

  public void setForename(String forename)
    {
        Forename= forename;
    }

  public void setSurname(String surname)
    {
        Surname= surname;
    }

  public void setMark(double mark)
    {
        Mark= mark;
    }

  public  String getForename()
    {
        return Forename;
    }

  public  String getSurname()
    {
        return Surname;
    }

  public  double getMark()
    {
        return Mark;
    }
  public  String getGrade()
    {   
            if ( Mark < 40 )
                Grade = "FAIL";
            else if ( (Mark >= 40) && (Mark <= 64) )
                Grade ="PASS";
            else if ( (Mark >= 65) && (Mark <= 84) )
                Grade ="MERIT";
            else if ( (Mark >= 85) && (Mark <= 100) )
                Grade ="DISTINCTION";
            return Grade;
}
  }

4 Answers 4

1

StudentClass = studentDetails.get(i); makes no sense. StudentClass is a classname.

You need an instance: StudentClass student = studentDetails.get(i); Then use student.getSurname() etc.

Sign up to request clarification or add additional context in comments.

Comments

0

StudentClass.getSurname() is not defined as a static method. You need to declare an instance of that class in order to be able to call it.

StudentClass myClass = new StudentClass(/* whatever params you need*/);
String surname = myClass.getSurname();

keep in mind you need to do this for all those methods, as they're at the instance level.

Comments

0
for(int i = 0; i < studentDetails.size(); i++){
             StudentClass = studentDetails.get(i);
             System.out.println( StudentClass.getForename() + " " +
             StudentClass.getSurname() + " received a " + StudentClass.getGrade() +
             " for their Student Mark of " + StudentClass.getMark() + "." );
            }

Change the code above to:

    for(int i = 0; i < studentDetails.size(); i++){
        StudentClass student = studentDetails.get(i);
        System.out.println( student.getForename() + " " +
        student.getSurname() + " received a " + student.getGrade() +
                 " for their Student Mark of " + student.getMark() + "." );
   }

See if the error will be gone. The compiler was complaining because you were trying to invoke an instance level (non-static) method on the class itself (rather than an instance of the class).

Comments

0

Non-static method in java cannot be accessed with out an object

Just try like this,

else{
         for(int i = 0; i < studentDetails.size(); i++){
             studentDetails.get(i) = new StudentClass();
             System.out.println( studentDetails.get(i).getForename() + " " +
             studentDetails.get(i).getSurname() + " received a " + studentDetails.get(i).getGrade() +
             " for their Student Mark of " + studentDetails.get(i).getMark() + "." );
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.