Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Java Class
Java Object
Java Object Array
Java Nesting of Classes
Java Methods
Java Method Overloading
Java Constructor
Java Constructor Overloading
Java This Keyword
Java Garbage Collection
Java Static Keyword
Java Arrays
Java String
Java StringBuffer
Java String vs StringBuffer
Java Vector Class
Java Vector vs Array
Java Wrapper Classes

Inheritance

Java Inheritance
Java Single Inheritance
Java Super Keyword
Java Multiple Inheritance
Java Hierarchical Inheritance
Java Multilevel Inheritance
Java Method Overriding
Java Abstract Classes,Methods
Java Final Variables, Methods
Java Dynamic Dispatch Methods
Java Visibility Controls

Interfaces

Java Interface
Java Class vs Interface
Java Multiple Inheritance
Java Nested Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Array of Object


Array of Objects Structure

Array of objects is the collection of objects of the same class.

Normally we declare the multiple objects of a class just like normal variables as
Student s1,s2,s3 ....sn;

Before going to learn array of objects first learn about the array in java

Syntax

classname arrayName[] = new classname[dimension];

classname : is the name of class which is defined

dimension : is the size of array starting from index 0 to n

Example

Creating the array of 5 Objects of Student class as

Student s1[] = new Student[5];

This will create array of 5 objects of student class.

We need to allocate memory for every object as

Student s1[0] = new Student();
Student s1[1] = new Student();
Student s1[2] = new Student();
Student s1[3] = new Student();
Student s1[4] = new Student();

This can be done by using loops as below

for(int i=0;i<5;i++)
{
	s1[i] = new Student();
}

Example

Define a class Student having data members rollnumber,name,marks. accept and display detailes of 5 students.

import java.util.Scanner;
class Student
{
     Scanner sc;
     String name;
     int rollno;
     float marks;
     void getData()
     {
	 sc = new Scanner(System.in);
	 System.out.print("Enter name : ");
	 name = sc.next();
	 System.out.print("Enter roll no : ");
	 rollno = sc.nextInt();
	 System.out.print("Enter marks : ");
	 marks = sc.nextFloat();
     }
     void display()
     {
	 System.out.println("\nName : "+name);
	 System.out.println("Roll no: "+rollno);
	 System.out.println("Marks : "+marks);
     }
     public static void main(String args[])
     {
	 int i;
	 Student s1[] = new Student[5];
	 for(i=0;i<2;i++)
	 {
	      s1[i] = new Student();
	      s1[i].getData();
	 }
	 System.out.println("\nStudent detailes are");
	 for(i=0;i<2;i++)
	      s1[i].display();
      }
}
	

Output

Enter Name : Daniel
Enter roll no : 59
Enter marks : 86.3
.
.
.
Student detailes are
Name : Daniel
Roll no : 59
Marks : 86.3
.
.
.

<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo