Hi all,
I've been doing some reading about calling a non static variable or method
from a static context, and almost but not quite have my head around what exactly is going on. I've read the text but can't seem to put that into context, so please bare with me.
I'm doing a program to simulate a student enrolment system (Unique huh!)
I have most of the functionality that I need, with exception to searching the array to
match based on an attribute but that I will work on later.
The project has a hard coded array of students which is then used to
search through for a matching year started at the place of study.
I'm having problem with my printEnrolment() method from my Enrolment class,
I used the addStudent() method in Enrolment to add the students to the array
using the Student constructor, and that works without drama.
I then put all of the information into a readable format using printDetails()
which returns a String which I then want to use in the printEnrolment() method under the Enrolment class, which is where I get the dreaded non static error.
Now from what I can tell, I somehow need to create an instance of the printDetails() method so that I can call it from the static printEnrolment() method, but i'm not quite sure as to how I should approach that.
Will my printEnrollment() method not work until I implement the search function?
Like, create a temp Student in the array to compare values against the array?
Is creating a temp student even the best way to achieve this?
Any advice to point me in the right direction would be very much appreciated!
Code below - Error line 23 of Enrolment
public class enrolment
{
public static void addStudent()
{
//Create objects of Student class
Student[] unistudent = new Student[6];
unistudent[0] = new Student("Al", "Capone", "Male", 12345671, "0419817271", 2006);
unistudent[1] = new Student("Bob", "Builder", "Male", 12345672, "0419817271", 2007);
unistudent[2] = new Student("Carol", "Candlelight", "Female", 12345673, "0419817271", 2007);
unistudent[3] = new Student("Dirk", "Diggler", "Male", 12345674, "0419817271", 2007);
unistudent[4] = new Student("Ebony", "Star", "Female", 12345675, "0419817271", 2008);
}
public void getStudent()
{
// Used for searchStudent() in main
}
public static void printEnrolment()
{
String details = Student.printDetails(); // <----- ERROR IS HERE
System.out.println(details);
}
}
class Student
{
private String firstName, lastName, gender, phoneNumber, details;
private int studentID,yearStarted;
public Student(String firstName, String lastName, String gender,
int studentID, String phoneNumber, int yearStarted)
{
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.studentID = studentID;
this.yearStarted = yearStarted;
this.phoneNumber = phoneNumber;
}
private String getFirstName()
{
return this.firstName;
}
private String getLastName()
{
return this.lastName;
}
private String getGender()
{
return this.gender;
}
private String getPhoneNumber()
{
return this.phoneNumber;
}
private int getStudentID()
{
return this.studentID;
}
private int getYearStarted()
{
return this.yearStarted;
}
public String printDetails()
{
String details = ("First Name: " + getFirstName() + "\nLast Name: " +getLastName() +
"\nStudent ID: " + getStudentID() + "\nGender: " + getGender() +
"\nPhone No: " + getPhoneNumber() + "\nYear Started: " + getYearStarted() +"\n\n");
return this.details;
}
}
public class admin
{
public static void main(String args[])
{
fillData();
}
public static void fillData()
{
enrolment.addStudent();
enrolment.printEnrolment();
}
public void searchStudent()
{
// Not yet started
}
}