+ 11

Object array

e.g if we have one class books and we declare array books[] my = new books [5]; my [0] = new book (); what will this do??? can anyone explain

23rd Feb 2017, 8:32 AM
adeel salim
adeel salim - avatar
7 Answers
+ 11
@mehdi but what is the purpose of doing this when my [0] is already an object?
23rd Feb 2017, 9:35 AM
adeel salim
adeel salim - avatar
+ 11
@Ettienne thanks alot. that makes it crystal clear
23rd Feb 2017, 10:49 AM
adeel salim
adeel salim - avatar
+ 10
still not sure if i get the point but its okay
23rd Feb 2017, 9:38 AM
adeel salim
adeel salim - avatar
+ 2
You are reassigning the my[0] reference in the array to point to a new book. The previous reference is overwritten, meaning the previous book does not now have any references pointing to it - it is now basically 'garbage' and the GC will collect it on its next GC cycle (i.e. run its finalizer and then delete it).
23rd Feb 2017, 10:48 AM
Ettienne Gilbert
Ettienne Gilbert - avatar
+ 1
its because where my[0] should store object can store in object array
23rd Feb 2017, 9:37 AM
Mehdi
Mehdi - avatar
+ 1
just do it. 😇
23rd Feb 2017, 9:40 AM
Mehdi
Mehdi - avatar
0
your books array should be extends object and its will work successfully
23rd Feb 2017, 9:33 AM
Mehdi
Mehdi - avatar