Topics Index

Classes >
Siva Nookala - 12 Apr 2016
The program Java Code To Print Student Details Using Arrays has some problems like not able to add, modify or delete the student since the information related to one student is present in multiple arrays. We can use classes to group this related data of the student into a Student class.

The below program shows how to print student details by using classes instead of arrays.

Print Student Details Using Classes
class PrintStudentDetailsUsingClasses
    public static void main(String s[])
        Student students[] = new Student[5];
        students[0] = new Student();
        students[0].name = "Rajesh";
        students[0].marks = 45;
        students[0].section = 'A';
        students[1] = new Student();
        students[1].name = "Suresh";
        students[1].marks = 78;
        students[1].section = 'B';
        students[2] = new Student();
        students[2].name = "Ramesh";
        students[2].marks = 83;
        students[2].section = 'A';
        students[3] = new Student();
        students[3].name = "Kamlesh";
        students[3].marks = 77;
        students[3].section = 'A';
        students[4] = new Student();
        students[4].name = "Vignesh";
        students[4].marks = 93;
        students[4].section = 'B';
        for(int i = 0; i < students.length; i++)
            System.out.println( students[i].name + " in section " + students[i].section + " got " + students[i].marks + " marks." );


class Student
    String name;
    int marks;
    char section;
    String address;
    String mobile;

Rajesh in section A got 45 marks.
Suresh in section B got 78 marks.
Ramesh in section A got 83 marks.
Kamlesh in section A got 77 marks.
Vignesh in section B got 93 marks.


Here we have a created an array of 5 Students. Since Student is class (i.e. non-primitive data type), we need to create the Student object using the new keyword after it is declared. That is why five Student objects are created and the variables name, marks and section are initialized separately. As usual, accessing of the variables of student objects can be done using the dot (.) operator.

  • Add one more student Venkatesh in section B with marks 87 and see the output. Please note that the array size has to be increased from 5 to 6.
  • Remove the student Ramesh and see the output. Note that the array size has to be decreased from 5 to 4 and the index of Kamlesh is 2 and the index of Vignesh is 3.
  • Add additional variable percentage with double type to the Student class and initialize the percentages for all the students. Also change the print statement to include the percentage as well.
Although the details of students are grouped in one place, the initialization of the data is still cumbersome and there are chances for the mistakes to happen. Also since the details like name, marks and section are required for every student, Java provides a mechanism to ensure that these values are initialized when the object is created. This mechanism is called Create Objects Using Constructors In Java.

Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.