Array of objects in Java

Arrays are defined as block of continous memory locations which are arranged in one after another. Elements of the Array can be accessed by the index number as:  a[2]


data_type[] array_name = new data_type[number of elements];


int[] name = new int[10];

Array of objects

Objects are instance of a class.

Object can easily be created using following syntax: class_name object_name = new class_name();

                                                                     Example: Student name = new Student();

but what if we need to create hundreds of objects of the same class. However this can be achieved using above procedure but imagine the the efforts and time required. To accomplish this task, Java introduces array of objects.

syntax: class_name[] object_name = new class_name[number of objects];

Example: Student name = new Student[5];

this code segment will create 5 objects called 'name' of type Student.

here is a beautiful example of apllication of array of objetcs: Students marksheet

Programs states:

Write a java program to accept details of students such as name, ID no, marks in Maths, Physics, and Chemistry. Display the students detail in descending order of their total marks. (Use array of objects)

import*;         //importing input-output files
class Student
   String name;                     //declaration of variables
   int id;
   int mathmarks;
   int phymarks;
   int chemmarks;
   int total;

   Student(String naam,int idno, int m,int p,int c)          //Initializing variables to user data
    void display()                             //displaying information


class Studentexe                     //main class
  public static void main(String args[]) throws IOException                  //exception handling
         System.out.println("Enter the numbers of students:");
         BufferedReader in=new BufferedReader(new InputStreamReader(;
         int n=Integer.parseInt(in.readLine());

         Student[]  S=new Student[n];                      // array of objects declared and defined
         for(int i=0;i         {
               System.out.println("Enter the Details of Student no: "+(i+1));             //reading data form the user
               System.out.println("Name: ");
               String nm=in.readLine();
               System.out.println("ID no: ");
               int idno=Integer.parseInt(in.readLine());
               System.out.println("Maths marks: ");              
               int m=Integer.parseInt(in.readLine());
               System.out.println("Physics marks: ");
               int p=Integer.parseInt(in.readLine());
               System.out.println("Chem marks: ");
               int c=Integer.parseInt(in.readLine());
              S[i]=new Student(nm,idno,m,p,c);                           //calling Student constructor
         Student temp;                           //swaping to achieve decsending order of total marks
         for(int a=0;a         {
                 for(int b=0;b                 {
                      if(S[b].total                      {
        System.out.println("\nName"+"\t"+"ID"+"\t"+"Math"+"\t"+"Phy "+"\t"+"Chem"+"\t"+"Total");            //printing data on the output screen
        for(int i=0;i        {


     }    //main ends here
}        //Studentexe class ends here


Note: 1) save the above code as '' while executing.


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet

Do you think Don 2 will be a hit?

Do you think Don 2 (hindi movie played by SRK)will be a hit

No answer selected. Please try again.
Please select either existing option or enter your own, however not both.
Please select minimum 0 answer(s) and maximum 2 answer(s).
[{"id":"21152","title":"Yes","votes":"69","type":"x","order":"0","pct":66.35,"resources":[]},{"id":"21153","title":"No","votes":"35","type":"x","order":"0","pct":33.65,"resources":[]}] ["#ff5b00","#4ac0f2","#b80028","#eef66c","#60bb22","#b96a9a","#62c2cc"] ["rgba(255,91,0,0.7)","rgba(74,192,242,0.7)","rgba(184,0,40,0.7)","rgba(238,246,108,0.7)","rgba(96,187,34,0.7)","rgba(185,106,154,0.7)","rgba(98,194,204,0.7)"] 350
bottom 200
No married couple wants to end up getting divorced. It is not like they have planned for it. They try to put up with their partners for as long as they
Due to our modern lifestyle, we feel that digestion related disorders are a common problem. Thus, we neither give importance to them nor seek any help
The bond of marriage brings the two people together. Initially, everything may seem okay and both of them slowly start discovering each other in the journey.
Why to choose Java? In the computer market, being able to use Java is a crucial knowledge. Java is a development terminology used to build compelling
DATABASE CONNECTIVITY This articles explains the need of database connectivity, the way it is achieved through Java using the JDBC(java database connectivity)
  Write your first simple java program.   First of all we need is a placewhere we can write your Java code. All Java programs are written using
Synchronized block is alternative mechanism for "synchronized" methods.If we inherihate the non synchronized blocks either from base class or interface
1) Go to the Path: C:Program FilesApache GroupTomcat 4.1webapps Create the following directory structure Exampledate ( it’s a Folder Name) META-INF