question for java tutorial 55

+1 Jason Yu · December 5, 2014
hi bucky, 
one question for tutorial 55
food bucky[] =new food[2]; 
does this mean two food objects are created?

Post a Reply

Replies

Oldest  Newest  Rating
0 Pere Garau Burguera · December 5, 2014
Not really, this only means you are creating an array of 2 elements. No objects are created here, if you try to access bucky[0] or bucky[1] you will get an error. To actually assign these variables an object, you should do something like:

for(int i=0; i<bucky.length; i++){
bucky = new Apple();
}

This way, when you do new food you are actually creating an array that will contain 2 elements, but doesn't contain anything yet, To assign the array positions an actual object, you have to do something like the for loop (or similar things). This will create an object (will run the constructor for that class), and assign it to an specific location into the array. 
0 Jason Yu · December 5, 2014
thanks, it is more clear right now.
any other question. how can i paste my code like your?
0 Pere Garau Burguera · December 6, 2014
At the bar that appears above the text box, just press the button that says code (8th from the right). It's a good practice to use this on the forum, so if you have any more questions, if you use code tags people will be able to see your code better. :)
0 Jason Yu · December 6, 2014
got it! 
thanks:D
  • 1

Java / Android Development

103,567 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator