array of objects in java
obj array[] = new obj[10];
for (int i = 0; i < array.length; i++) {
array[i] = new obj(i);
}public class MainClass
{
public static void main(String args[])
{
System.out.println("Hello, World!");
//step1 : first create array of 10 elements that holds object addresses.
Emp[] employees = new Emp[10];
//step2 : now create objects in a loop.
for(int i=0; i<employees.length; i++){
employees[i] = new Emp(i+1);//this will call constructor.
}
}
}
class Emp{
int eno;
public Emp(int no){
eno = no;
System.out.println("emp constructor called..eno is.."+eno);
}
}//create class
class enemies {
int marks;
}
//create object array
enemies[] enemiesArray = new enemies[7];
//assign value to object
enemiesArray[5] = new enemies(95);A[] arr = new A[4];
for (int i = 0; i < 4; i++) {
arr[i] = new A();
}
//test
Also in Java:
- write a java program to check whether given number is binary or not
- treeset java descending order using comparator
- android image view showing rotate image
- string replace last character java
- java add a list to a list
- how to use the this keyword in java
- string reverse in java
- java substring
- print a list java
- java string contains char
- JavaFX mouse over effe
- split with multiple condition in java
- how to install java runtime environment on centos 7
- get tfidf score for a sentence
- java regex replace all characters before
- check java version cmd
- Create ArrayList from array java
- goodbye java
- java bigdecimal third root
- java get command line input
- how to declare a linked list in java
- how to change actionbar color in android programmatically
- google translate
- get first 5 characters of string java