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 Data Structures Getting There Packages

Liron Tal
Liron Tal
4,825 Points

How does Treet treet = new Treet(); work?

How is it possible that although Treet.java does not have a constructor, the Treet treet = new Treet(); works?

Moreover, what's com.teamtreehouse.Treet@7852e922 means?

2 Answers

Christopher Augg
Christopher Augg
21,223 Points

Liron,

All classes have a default constructor even if you do not declare one. That is why you can instantiate the Treet class as you have written. As for your second question, all Java objects have a toString() method that is defined in the Object class. This is the parent class or superclass of all Java objects. When you use a print method with an object and you did not Override the toString method for that object, it will print the class name, @ symbol, and the object's hexadecimal hash code. I will leave the rest to Google as I am sure this might open up a lot more questions. I hope it helps though.

Regards,

Chris

Kane Stoboi
Kane Stoboi
21,919 Points

The constructor of an object is simply a method of the object, usually used to set initial values. The constructor method is run when the object is created. If you do not need any code to be run on the creation of an object than you do not need a constructor method.