-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31_ArrayOfObjects
More file actions
38 lines (31 loc) · 832 Bytes
/
31_ArrayOfObjects
File metadata and controls
38 lines (31 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* To create array
int[] numbers = new int[3];
char[] characters = new char[4];
String[] strings = new String[5];
*/
<Main.java>
public class Main {
public static void main(String[] args){
/* Food[] refrigerator = new Food[3]; */
Food food1 = new Food("pizza");
Food food2 = new Food("hamburger");
Food food3 = new Food("hotdog");
Food[] refrigerator = {food1, food2, food3};
/* refrigerator[0]=food1;
refrigerator[1] = food2;
refrigerator[2] = food3;
System.out.println(refrigerator[0]);
>>Food@27d6c5e0
*/
System.out.println(refrigerator[0].name);
System.out.println(refrigerator[1].name);
System.out.println(refrigerator[2].name);
}
}
<Food.java>
public class Food {
String name;
Food(String name){
this.name = name;
}
}