How to make objects array in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to make objects array in Java?

21st Oct 2018, 1:55 PM
S.K
4 Answers
+ 12
Assuming you have a class: class A { //... } You'll declare something like: A[] obj = new obj[100];
21st Oct 2018, 2:05 PM
Hatsy Rei
Hatsy Rei - avatar
+ 8
Here some code with example: https://code.sololearn.com/c6JIMQd7H3g4
21st Oct 2018, 2:19 PM
Anya
Anya - avatar
+ 4
you can declare explicitly too: name_class[] my_array={value_0,...value_n}; example: int[] a={0,-1,5555};
21st Oct 2018, 10:16 PM
AntonioJFN
AntonioJFN - avatar
+ 4
Declaration – In this step, we specify the data type and the dimensions of the array that we are going to create. But remember, we don't mention the sizes of dimensions yet. They are left empty. Instantiation – In this step, we create the array, or allocate memory for the array, using the new keyword. It is in this step that we mention the sizes of the array dimensions. Initialization – The array is always initialized to the data type’s default value. But we can make our own initializations. Declaring Arrays In Java This is how we declare a one-dimensional array in Java – int[] array; int array[]; Oracle recommends that you use the former syntax for declaring arrays. Here are some other examples of legal declarations – // One Dimensional Arrays int[] intArray; // Good double[] doubleArray; // One Dimensional Arrays byte byteArray[]; // Ugly! long longArray[]; // Two Dimensional Arrays int[][] int2DArray; // Good double[][] double2DArray; // Two Dimensional Arrays byte[] byte2DArray[]; // Ugly long[] long2DArray[]; And these are some examples of illegal declarations – int[5] intArray; // Don't mention size! double{} doubleArray; // Square Brackets please! Instantiation This is how we “instantiate”, or allocate memory for an array – int[] array = new int[5]; When the JVM encounters the new keyword, it understands that it must allocate memory for something. And by specifying int[5], we mean that we want an array of ints, of size 5. So, the JVM creates the memory and assigns the reference of the newly allocated memory to array which a “reference” of type int[] Initialization Using a Loop – Using a for loop to initialize elements of an array is the most common way to get the array going. There’s no need to run a for loop if you are going to assign the default value itself, because JVM does it for you. https://crbtech.in/java-training/java-certification-course/
22nd Oct 2018, 6:54 AM
meenal deshpande