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

default constructor didn't want to define?

in privacy and method video... we create a object using default constructor.. PezDispenser dispenser = new PezDispenser(); so my question is that we create a object or instance of PezDispenser. Using default constructor ..but in another class why we dont define our pezDispenser constructor?

2 Answers

Alexander Nikiforov
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,161 Points

When we write PezDispencer pezDispencer = new PezDispenser() in Example class we are creating instance of PezDispencer class in Example class. I hope I understand you correctly. We separate classes deliberately, so that PezDispencer class is defined in another file. We don't need to define constructor in Example class because, constructor is already defined in PezDispenser class.

Another way to think about it: we separate all classes in different files and even packages. We put definitions of them, their methods and constructors there.

But We also have (in console app) one "Main" class with public static void Main()... method (Example in this case) in which all classes will be combined together and used. I hope I understand you correctly, if not, try to be rewrite your question more specific...