1obj array[] = new obj[10];
2
3for (int i = 0; i < array.length; i++) {
4 array[i] = new obj(i);
5}
1public class MainClass
2{
3 public static void main(String args[])
4 {
5 System.out.println("Hello, World!");
6 //step1 : first create array of 10 elements that holds object addresses.
7 Emp[] employees = new Emp[10];
8 //step2 : now create objects in a loop.
9 for(int i=0; i<employees.length; i++){
10 employees[i] = new Emp(i+1);//this will call constructor.
11 }
12 }
13}
14
15class Emp{
16 int eno;
17 public Emp(int no){
18 eno = no;
19 System.out.println("emp constructor called..eno is.."+eno);
20 }
21}
1class ObjectArray{
2 public static void main(String args[]){
3 Account obj[] = new Account[2] ;
4 //obj[0] = new Account();
5 //obj[1] = new Account();
6 obj[0].setData(1,2);
7 obj[1].setData(3,4);
8 System.out.println("For Array Element 0");
9 obj[0].showData();
10 System.out.println("For Array Element 1");
11 obj[1].showData();
12 }
13}
14class Account{
15 int a;
16 int b;
17 public void setData(int c,int d){
18 a=c;
19 b=d;
20 }
21 public void showData(){
22 System.out.println("Value of a ="+a);
23 System.out.println("Value of b ="+b);
24 }
25}