Ahsan Ali Ahsan Ali - 2 months ago 6
Java Question

How to get input in array of class in java?

I am new to java and this is our first assignment but i am unclear right now that why didnt our teacher passes a array of class object to the Add Student method instead he created array of object as private and just passed the name of the array.How can i add data of students now

public class StudentManagement {
private Student students [] ;
private int CAPACITY ;
private int CURRENT_POSITION;
public StudentManagement(){
CAPACITY = 3;
CURRENT_POSITION = 0;
students = new Student[CAPACITY];
}

public void addStudent(Student student){


}

Answer

As commented by Turing85, Please talk to your instructor to understand the concepts.

This array can hold 3 objects (CAPACITY).

CURRENT_POSITION is where you are going to add your object.

You can try similar to this..

public void addStudent(Student student) {
    if (CURRENT_POSITION < CAPACITY) {
        students[CURRENT_POSITION] = student;
        CURRENT_POSITION++;
    }
}