If we want to deal with large amount of data of same datatype then instead of declaring every single variable as variable0, variable1, variable2, etc we can rather declare an array of that datatype and store the data in it.
In java object is an instance of a class. Java supports encapsulation of data i.e. from class1 you cannot directly call a method of class2. For this you need to create an object of class2 and using that object you have to call that method of class2.
In this example,
- We will be creating a class Student which will contain a String variable name and an Integer variable age.
- We will then create an array of type Student.
- Then we will create objects of class Student, store values in them and then store these objects in the array which we created.
This is a video that explains this topic in short.
/*creating a parameterized constructor to assign values to new objects*/
public Student(String name,int age)
public class StudentDemo
public static void main(String args)
/*creating an array which stores references of objects of type Student*/
Student students=new Student;
/*creating objects of class Student and passing values through a parameterized constructor and storing these objects in array*/
System.out.println(“Name: “+students[i].name+”, age: “+students[i].age);