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 trialSachu AA
526 PointsWhy Did Craig used " new " before "PezDispenser()" ?
Plz someone Help me with this "new " command , which is new to me !
1 Answer
Chase Marchione
155,055 PointsHi Sachu,
The new keyword is used in Java to create a new instance of a class (in other words, an object.) By using the new keyword, we are saying "This object is a brand new object that we are creating right now.", and in this particular case it is a PezDispenser object that Craig named dispenser.
In the following statement:
PezDispenser dispenser = new PezDispenser();
On the left side we are saying 'We are going to set something to a PezDispenser object named dispenser', and on the right side we are instantiating (creating) that object. To summarize, we are both creating this PezDispenser object named dispenser, and initializing it: as being a new PezDispenser object.
Hope this helps!
Sachu AA
526 PointsSachu AA
526 PointsThanks A Lot @CJ Marchione