- How to Create Array of Objects in Java
- How to Create Array of Objects in Java?
- Create array java objects
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- Create an Array of Objects in Java
- Create an Array of Objects and Initialize Objects Using a Constructor in Java
- Create an Array of Objects and Initialize the Objects Calling the Constructor Using the <> Array Notation in Java
- Declare an Array of Objects With Initial Values in Java
- Related Article - Java Object
- How to Create an Array of Objects in Java
- How to Create an Array of Objects in Java?
- Method 1: Create an Array of Objects in Java Using Declaration Process
- Example: Declaring an Array of Object of One Class in Another Class
- Method 2: Create an Array of Objects in Java Using Declaration and Instantiation Process
- Example: Declaring and Instantiating an Array of Object of One Class in Another Class
- Method 3: Create an Array of Objects in Java Using Declaration and Initialization Process
- Example: Declaring and Initializing an Array of Object of Predefined Object Class
- Conclusion
- About the author
- Farah Batool
How to Create Array of Objects in Java
Java Array Of Objects, as defined by its name, stores an array of objects. Unlike a traditional array that store values like string, integer, Boolean, etc an array of objects stores OBJECTS. The array elements store the location of the reference variables of the object.
Class obj[]= new Class[array_length]
How to Create Array of Objects in Java?
Step 1) Open your code editor.
Copy the following code into an editor.
class ObjectArray < public static void main(String args[])< Account obj[] = new Account[2] ; //obj[0] = new Account(); //obj[1] = new Account(); 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 /java-variables.html" data-lasso-id="471893">variables as shown below.
Step 5) Uncomment Line.
Uncomment Line # 4 & 5. This step creates objects and assigns them to the reference variable array as shown below. Your code must run now.
Step 5) Uncomment Line.
Uncomment Line # 4 & 5. This step creates objects and assigns them to the reference variable array as shown below. Your code must run now.
Output:
For Array Element 0 Value of a =1 Value of b =2 For Array Element 1 Value of a =3 Value of b =4Create array java objects
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
Create an Array of Objects in Java
- Create an Array of Objects and Initialize Objects Using a Constructor in Java
- Create an Array of Objects and Initialize the Objects Calling the Constructor Using the <> Array Notation in Java
- Declare an Array of Objects With Initial Values in Java
This article will introduce methods to create an array of objects in Java. The article will also demonstrate the instantiation of objects and their implementation.
Create an Array of Objects and Initialize Objects Using a Constructor in Java
Java is an object-oriented programming language, and it consists of classes and objects. We can create an array of an object using the [] array notation in Java. We can use the constructor to initialize the objects by passing the values to it. The syntax of the expression is shown below.
Type[] objectName = new ClassName[];
The Type denotes the type of the object. It may be of a specific data type or a class type. The [] symbol after the type resembles that we are creating an array. The option objectName refers to the name of the object. The new operator creates an instance. The ClassName refers to the name of the class whose object is made. We can specify the size of the array in the [] after the class. We can use the index in the array to instantiate each object.
- Create a class Store to write the main method to it. Inside the main method, create an array arr of the Customer type and allocate the memory for two Customer classes’ objects.
- Create two objects of the Customer class from the indexed array arr .
- Supply the values 709270 and Robert for the first object and 709219 and Neal for the second object while creating the object.
These values are the parameters for the constructor of the Customer class. Then call the display() function with the created objects.
The Customer class contains the public properties id and name . The constructor of the class sets the values for these properties. The public function display() displays the property of the class. In the example below, the objects are created from an array, and the constructor is invoked during the creation of the object. Then, the objects call the display() function, and the output is displayed. So far, we have learned how to create an array of objects and use it with the methods.
public class Store public static void main(String args[]) Customer[] arr = new Customer[2]; arr[0] = new Customer(709270, "Robert"); arr[1] = new Customer(709219, "Neal"); arr[0].display(); arr[1].display(); > > class Customer public int id; public String name; Customer(int id, String name) this.id = id; this.name = name; > public void display() System.out.println("Customer id is: " + id + " " + "and Customer name is: " + name); > >
Customer id is: 709270 and Customer name is: Robert Customer id is: 709219 and Customer name is: Neal
Create an Array of Objects and Initialize the Objects Calling the Constructor Using the <> Array Notation in Java
In the second method, we will create an array of objects as we did in the first method. That is, we will be using the constructor to instantiate the objects. But we will use a single-line approach to instantiate the objects. We will call the constructor the time we create the array to hold the objects. We can write the constructor call in a single line inside the <> array notation. We will create the objects of the Customer class in the Store class.
For example, create an array arr as in the first method. But instead of allocating the memory for the objects, create the objects in the same line. Write an array <> notation after the new Customer[] . Next, create two objects of the Customer class with the new keyword. Supply the respective id and name as the parameters to the constructor. Use a comma to separate each constructor call.
public class Store public static void main(String args[]) Customer[] arr = new Customer[] new Customer(709270, "Robert"), new Customer(709219, "Neal")> ; arr[0].display(); arr[1].display(); > > class Customer public int id; public String name; Customer(int id, String name) this.id = id; this.name = name; > public void display() System.out.println("Customer id is: " + id + " " + "and Customer name is: " + name); > >
Customer id is: 703270 and Customer name is: Sushant Customer id is: 703219 and Customer name is: Simanta
Declare an Array of Objects With Initial Values in Java
In the third method of creating an array of objects in Java, we will declare an array of objects providing the initial values. We will not create another class object in this approach. So, there will be no use of the constructor in this method. We will use the array <> notation to write the array of objects. We will use the Object type to create the array of objects.
For example, create a class and write the main method. Then, create an array arr of the Object type. Write the objects inside the <> notation. The objects are CRF , a string value, an instance of the Integer class with the value of 2020 , Husky another string, and another instance of the Integer class with value 2017 . Finally, print each of the objects using the array indexing method.
class Motorcycle public static void main(String args[]) Object[] arr = "CRF", new Integer(2020), "Husky", new Integer(2017) >; System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); System.out.println(arr[3]); > >
Related Article - Java Object
How to Create an Array of Objects in Java
When you need to store a single object in your program, you can use an Object variable. However, while dealing with a large number of objects, it is preferable to use an Array of Objects. It is important to note that when we say Array of Objects, we are referring to the object’s reference rather than the actual objects. The “[]” array notation in Java can be used to create an array of an object.
This guide will describe the procedures for creating Java array objects.
How to Create an Array of Objects in Java?
For creating an array of objects in Java, you can use the following approaches:
Let’s come to understand these methods with detailed examples.
Method 1: Create an Array of Objects in Java Using Declaration Process
In Java, the array of objects is created the same as the array creation process. In this section, we will create an array of objects using the declaration process.
The syntax for declaring an array of objects is given below.
Follow the below-given syntax to create an array of objects:
In Java, the class is also a user-defined data type.
You can also follow the below syntax:
Example: Declaring an Array of Object of One Class in Another Class
In this example, first we will create a “User” class that contains two variables, “id” and “Name”, a parameterized constructor and a method named “display()” that displays the variable values on the console:
classUser {
int id ;
String Name ;
User ( String name, int id ) {
this . Name = name ;
this . id = id ;
}
publicvoiddisplay ( ) {
System . out . print ( "Name is " + Name + " " + "and the id is " + id ) ;
System . out . println ( ) ;
}
}
Now, in the main() method of another class named “Example”, first we will create an array of objects of “User” type by declaring an array that stores the objects. Then, we will allocate memory for objects with length “5”. After that, we will initialize the values for each index of the array and display the value of index “2” by calling the “display()” method of the “User” class:
publicclassExample {
publicstaticvoidmain ( String [ ] args ) {
User [ ] user ;
user = new User [ 5 ] ;
user [ 0 ] = new User ( "John" , 1 ) ;
user [ 1 ] = new User ( "Karley" , 2 ) ;
user [ 2 ] = new User ( "Rohnda" , 3 ) ;
user [ 3 ] = new User ( "Byrne" , 4 ) ;
user [ 4 ] = new User ( "Kotley" , 5 ) ;
System . out . print ( "User data in array's index 2: " ) ;
user [ 2 ] . display ( ) ;
}
}
The output shows the value of the object at the 2nd index:
Let’s see another method for creating an array of objects.
Method 2: Create an Array of Objects in Java Using Declaration and Instantiation Process
In this section, we will create an array by declaring and instantiating it simultaneously. You can instantiate an array by using the “new” keyword and pass the length of the array in it. This approach is more efficient as it reduces the lines of code by handling the declaration and instantiation process at a time.
The syntax for declaring and instantiating an array of objects is given below:
Example: Declaring and Instantiating an Array of Object of One Class in Another Class
In this example, we will create an array of objects named “user” by declaring and instantiating it simultaneously:
Initialize the values to the objects as in the previous example and display the value of the object at index “3”, invoking “display()” method of the User class:
The output shows the value of the user object at the 3rd index:
Now, let’s see the last method for creating an array of objects.
Method 3: Create an Array of Objects in Java Using Declaration and Initialization Process
For creating an array of objects, you can also initialize at the time of declaration using “ ” curly braces:
The following syntax is used for the creation of an array of objects:
Here, we declare an array of class type and immediately initialize it with values.
Example: Declaring and Initializing an Array of Object of Predefined Object Class
In this example, we will create an array of objects of the “Object” type named “obj” and initialize it with values using “ ” curly braces. Here, Object is the predefined Java class that is used here as a type of array:
Then, we will print the value of the object at the 0th index using the “System.out.println()” method:
The output indicates that the “John” is stored at the 0th index of the array of objects “obj”:
We have gathered all the ways for creating an array of objects in Java.
Conclusion
For creating an array of objects in Java, you can use different approaches like declaration, a declaration with instantiation, and a declaration with initialization. In Java, the array of objects is created the same as the array creation process because an object’s array stores the objects. Java permits you to create an array of objects of both user-defined and predefined classes. In this guide, we described the ways for the creation of an array of objects with examples.
About the author
Farah Batool
I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.