how to initialize array in java
int[] arr = new int[5]; // integer array of size 5 you can also change data type
how to initialize array in java
int[] arr = new int[5]; // integer array of size 5 you can also change data type
creating array of objects in java
class Main{
public static void main(String args[]){
//create array of employee object
Employee[] obj = new Employee[2] ;
//create & initialize actual employee objects using constructor
obj[0] = new Employee(100,"ABC");
obj[1] = new Employee(200,"XYZ");
//display the employee object data
System.out.println("Employee Object 1:");
obj[0].showData();
System.out.println("Employee Object 2:");
obj[1].showData();
}
}
//Employee class with empId and name as attributes
class Employee{
int empId;
String name;
//Employee class constructor
Employee(inteid, String n){
empId = eid;
name = n;
}
public void showData(){
System.out.print("EmpId = "+empId + " " + " Employee Name = "+name);
System.out.println();
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us