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 trialRodrigo Gomez-Palacio
1,125 Pointscreating an object
I am also confused. There are too many combinations of the words 'PEZ' AND 'DISPENSER' in this video that it's kind of hard to keep track.
So to create an object, do we have to assume/know that a class exists of the same name as the object type we wish to create? At this point in our knowledge, what object types can we assume from the standard library?
In this code, java Dog max = new Dog();
the type is Dog, the name is max, and Dog() creates the dog. Why is the naming of the actual object of its same type? Like it's hard to understand that the type is also the name of the object. But the name is actually also "max"? Also, what parameters would go in the parentheses?
Thanks
1 Answer
David Lopez
3,211 PointsWhen you create a class in Java, and lets use your "Dog" example, it would look something like this:
public class Dog { //instance variables go here int variable;
//constructors go here and constructors are always the name of the class
Dog()
{
variable++;
}
//methods
...
...
}
so when you go to implement that class and create new instances of that class, the "new" operator uses the "blueprint" of your class to create a "new" instance of that class, using the constructor Dog() in that class to actually do it.
hence the code:
Dog spike = new Dog();
the first Dog is the class or blue print of the instance to be created spike is the instance of the Dog Class and is the new object being created Dog() is the constructor being used to create the "new" object...
Capiche?!
Jorge Flores
7,864 PointsJorge Flores
7,864 PointsHi Rodrigo, answering your first question, the name of the object does not inherit the name of the class, but you need to call first the name of the class in order to create the object, then you need to initialize it thats why you write new Dog(). you name the object in order to differentiate it from other other objects of the same type, let me explain more detailed how it works. Between the parentheses you add the parameters that fills the contructors. Remember when you created the class you added some methods that had the same name of the class, those are constructors, they initialize variables in the class. Lets say you have the variable String color; in the class, and you want to give it a value every time someone creates a new Dog object, then you need to add a constructor. that would look like this:
Public Dog(String newColor){ color=newColor; } Having this, every time someone creates a Dog boject they will need to fill the parentheses with a String variable(this variable will be newColor in the class), then that variable pass the value to the member variable on the class, thats why we added the line : color = newColor;
Going back to the original question, when you created the Dog object you also gave it a color, but if you want to have 2 Dog objects with diferent colors, you just need to create a new object with a new color on the parentheses parameter, thats why you need to name the object diferently, because you may want to have a Brown Dog named Max and a Yellow Dog named Fido.