Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Meet Objects Constructors

1 Answer

Patrick Taschner
Patrick Taschner
7,176 Points

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.