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 Java Objects (Retired) Meet Objects Constructors

1 Answer

Hey Mayur, constructors in Java is a block of code which is called when an instance of an object is created ( Car car = new Car(); ). The "Car();" calls the constructor. Here are the key differences between a constructor and a method:

-A constructor doesn’t have a return type.

-The name of the constructor must be the same as the name of the class.

-Unlike methods, constructors are not considered members of a class.

-A constructor is called automatically when a new instance of an object is created.

Constructors are mostly used to initialize the variables of a class, without using an extra setter-method.

https://www.youtube.com/watch?v=tPFuVRbUTwA

This video might be helpful :-)