Topics Index

Classes >
Siva Nookala - 12 Apr 2016
Classes are supported by every object oriented programming language and they can be used to group related data. For e.g., if we take student details, although we can store the information about the student using primitive data types like int, double and char, it will be useful to have a separate type for student.

Print student details using arrays
class PrintStudentDetailsUsingArrays
    public static void main(String arg[])
        String names[] = { "Rajesh", "Suresh", "Ramesh", "Kamlesh", "Vignesh" };
        int marks[] = { 45, 78, 83, 77, 93 };
        char sections[] = { 'A', 'B', 'A', 'A', 'B' };
        for(int i = 0; i < names.length; i++)
            System.out.println( names[i] + " in section " + sections[i] + " got " + marks[i] + " marks." );

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 created arrays names, marks and sections to store the student details. We used a for loop to iterate through the elements and print the details. The lengths of the all the arrays are same, hence the first element (index 0) in names correspond to first element in sections and marks array. Similarly the second element (index 1) in names correspond to second element in sections and marks array. So if we want Ramesh details we need to look at 3rd element (index 2) in every array.

  • Add the details of another student Venkatesh in section B with marks 87 and see the output.
  • Remove the details of Ramesh i.e. remove "Ramesh" from names array, remove 83 from marks array and remove the second 'A' in sections array and see the output.
  • Remove the name of student Suresh from names array, but keep his marks and sections and see the output. Observe that the output does not print the marks and sections correctly.
  • Remove the marks from marks array and section from sections array for the student Kamlesh, but keep his name in the names array. When we do this it throws an ArrayIndexOutOfBoundsException since we do not have the section and name corresponding to the student.
The above program prints the student details, if we want to store additional students or edit the student information, it is risky and error prone. Than storing the details of one student in multiple arrays, it would be better to group all the information belonging to a student in one place. This can be done using classes. A class is a user defined data type comprising of smaller data elements. For e.g., a student class contains other data like name, marks and section. This way all related information of student can be grouped in one place. The syntax for creating a class with data is

class classname
    type variable1;
    type variable2;
    type variable3;
The class is a keyword and the classname is the name of the class. A class can contain multiple variables (or elements) and each variable having a type like int, char, double and every variable has a different name. So if we want to create a Student class, it will look like this.
class Student
    String name;
    int marks;
    char section;
And similarly a Car class will look like.
class Car
    String owner;
    String registrationNumber;
    int numberOfSeats;
    double engineCC;
    double mileage;
As you can see a class is comprised of primitive data types and any other non primitive data types. For e.g., Car is comprised of two Strings, one int and two doubles. int and double are primitive data types, but String is non-primitive data type, which is again comprised of some other primitive data types. Non-primitive data types are also referred as user defined data types. Creating variables of non-primitive data types is different from primitive data types and it is explained in Java Classes and Java Objects.

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App