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
Eric Tang
Courses Plus Student 1,300 PointsThe brackets at the end of new PezDispenser
Hi, in the video creating classes Craig talks about creating an object with the code PezDispenser dispenser = new PezDispenser ();
This really confuses me as I've seen a couple of codes from other videos having this brackets and have no idea what it means. What is the purpose of the brackets and when do I use them?
2 Answers
andren
28,558 PointsThose brackets (which are usually referred to as parentheses in programming, since brackets is a bit too general) are used when you call a method. When you create an object you do so by calling the class' constructor, which is a special type of method that is responsible for creating the object from the class.
These are somewhat complex topics that will be expanded upon in more detail as the C# track goes on, so you don't need to be too worried if you don't really understand much of it right now.
Eric Tang
Courses Plus Student 1,300 PointsI think I am starting to see the picture now. Thank You so much Andren and sorry I replied so late, my internet was down. Will do! Thanks again!
Eric Tang
Courses Plus Student 1,300 PointsEric Tang
Courses Plus Student 1,300 PointsThanks for that Andren. I've actually watched up to the constructor video and everything so far has been a blur. So I'm re watching everything . Also, there's still so many terms that I am still unfamiliar with . Could you please quickly explain to me in a nutshell the relationship between a class, method and object?
andren
28,558 Pointsandren
28,558 PointsSumming things up succinctly is sadly not my strong point, but if you want a somewhat simple (but long) explanation then I have written some stuff below that might help clarify what methods, objects and classes are all about.
A method is essentially just a collection of code that can be reused thought your application. So if you write some complex code to do some task that could be useful to run at various point in your program you can use a method.
A method can also be given data when you call it (though parameters/arguments) which means that the method can work with different data each time you call it.
There is a popular principle in programming called DRY which stands for "Don't repeat yourself", and it essentially means that you should never find yourself writing code that performs the same type of tasks multiple times in the same program. Methods make it easier to adhere to that principle.
A class is also a type of collection. It is a collection of methods, and properties, which are essentially just variables that are associated with a class rather than a method. The methods in a class can take in data when they are called (like normal methods) but can also make use of the properties that are defined in the class.
Methods and properties in a class also have access levels, the most common ones being
publicandprivate. Apublicmethod/property can be referenced both by methods within the class and by methods outside the class, aprivatemethod/property can only be accessed from within the methods of the class. Which means that you have a greater amount of control over them.A constructor is a method that is responsible for creating an object from the class, and it can also be passed data through parameters/arguments just like a regular method that can then be used to setup some of the properties of the class.
The real use of classes comes from the fact that you can instantiate them, which is where objects come in.
An object is an instance of a class, put more simply it is a copy of the class that keeps it's own independent state. Meaning that if you have a class with a variable called
nameand then create two objects from that class they will both have anameproperty that is independent of each other. Meaning you can change thenameproperty of the first object without that affecting the second object.One example of where a class could be useful is for example to represent a player in a video game. You could define all of the stats of the player as properties, and then have methods that affected those stats in various ways, then you could create one object for each player in the game.
I hope this clarified some things for you, if you have further questions feel free to ask me.