Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java The Solution

michael lee
michael lee
5,179 Points

Cow[] cows = new Cow[names.length];

can anybody explain what each of the three cows in this code stand for?

Cow[] cows = new Cow[names.length];

michael lee
michael lee
5,179 Points

@turaboy thanks a lot!! very clear and understandable!

1 Answer

Cow[] -- is a type declaration( stating that upcoming variable will hold "the list of Cow class instances") it is simalar to saying: String[] mystring = ["one", "two", "three"] cows -- is a variable name (it could be anything) new Cow -- creates an instance of Cow class names.length -- is a number generated by evaluating the number of items in names array(this number states how many items should be in the array) (names array has already been defined and 'length' is a method used on arrays to find the number of items in an array)

Once that code runs: cows variable will hold an array of three Cow instances (because names array has three items in it) [Cow instance1 , Cow instance2 , Cow instance3 ]