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 trialAditya Gawade
1,182 Pointsi did not understand the purpose of init. also can u please explain the working of the code in th class? thnx
i did not understand the purpose of init. also can u please explain the working of the code in th class.
1 Answer
landonferrier
25,097 PointsAditya,
The "init" or initializer method is something you will see in every programming language. The purpose of the init method varies between and languages and on the intention of the programmer, but here's the general definition of an initializer method.
So, lets say you want to make a new instance of a class called "car." I know this is basic, but it is not easy to understand hard subjects without understanding the underlying components. Pretend we have already created the Car class, and now we want to spawn one in our game. We will call: car1 = Car.init("Ferrari", 200, "red"). In this initializer, the method is taking a car name, a top speed, and a color. Using this information, the Car class will make the room for storage, and return a new instance of the car already stored in memory. Now we can use the car.
I hope you found this useful. If you have any questions, feel free to ask!