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 trialKohei Ashida
4,882 PointsIn what kind of situations can class method be more useful than instance method?
I've read over the related questions but still am unclear about the advantages of class method than instance method though I understand that class method doesn't require to generate an instance to call the method like Bookcase.create_bookcase()
1 Answer
Steven Parker
231,198 PointsOne common use for a class method is when the method itself creates a new instance. That way, you can call it before having an instance, but get an instance back. This is used in some of the course lesson examples and challenges.
Kohei Ashida
4,882 PointsKohei Ashida
4,882 PointsThank you, Steven! Now I feel I have to get common uses through further lessons and challenges.
Katherina Kallis
3,385 PointsKatherina Kallis
3,385 PointsI thought class methods didn't create new instances? Now I'm puzzled.
Steven Parker
231,198 PointsSteven Parker
231,198 PointsThey certainly can, if you write code for it (it's not automatic).
Katherina Kallis
3,385 PointsKatherina Kallis
3,385 PointsThank you!