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

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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 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...

thanks..