array of object in java code example
Example 1: array objects java
obj array[] = new obj[10];
for (int i = 0; i < array.length; i++) {
array[i] = new obj(i);
}
Example 2: java array object
Class obj[]= new Class[array_length]
Example 3: how to make array of objects in java and use it
class enemies {
int marks;
}
enemies[] enemiesArray = new enemies[7];
enemiesArray[5] = new enemies(95);
Example 4: java class array of objects
public class MainClass
{
public static void main(String args[])
{
System.out.println("Hello, World!");
Emp[] employees = new Emp[10];
for(int i=0; i<employees.length; i++){
employees[i] = new Emp(i+1);
}
}
}
class Emp{
int eno;
public Emp(int no){
eno = no;
System.out.println("emp constructor called..eno is.."+eno);
}
}
Example 5: array object java
class ObjectArray{
public static void main(String args[]){
Account obj[] = new Account[2] ;
obj[0].setData(1,2);
obj[1].setData(3,4);
System.out.println("For Array Element 0");
obj[0].showData();
System.out.println("For Array Element 1");
obj[1].showData();
}
}
class Account{
int a;
int b;
public void setData(int c,int d){
a=c;
b=d;
}
public void showData(){
System.out.println("Value of a ="+a);
System.out.println("Value of b ="+b);
}
}