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 trialAditya Puri
1,080 PointsHow will the constructor return the class?
At 1:00 he says that the constructor returns the class "PezDispenser".How will the constructor return the class ?
How can it return a class? Isn't it the part of the class?
And how is constructor a method??
4 Answers
tobiaskrause
9,160 PointsHere we have an constructor. The constructor is, like you can see in the video, used to create in instance of a class. This is useful because you can give the fields of the class initial values.
public class Test {
private String testString;
public Test(String valueForTestString) {
testString = valueForTestString;
}
}
Test test = new Test("Some Test String");
// so now we give the constructor test the parameter "Some test String"
// as you can see above the testString will get this value
// we created an instance of the class Test with the name "test" with initial values for the private field testString
As you can see here test is a instance of the class Test because the constructor returns the same type of class
tobiaskrause
9,160 PointsSeems like you still don't understand what the usage of constructors is Maybe this will help you: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
Constructors are some kind of weird species. Maybe it is wrong to call them methods. Some people call it one because we call the operator new followed by the call of a construction method.
BY THE WAY not all methods give you a return value. Every void-method like you main method wont return anything.
Method method = new Method("Something");
Wrong...Class nameofInstance = new Class("Something"); We just create an instance of a class and give the constructor values for the init-values. There is a reason why constnructors have the same name like the class.
Aditya Puri
1,080 Pointsstill don't understand why the constructor has the same name as the class....
tobiaskrause
9,160 PointsBecause there is not any syntax or keyword for a constructor...it is just the desing/concept of constructors in serveral oop langauges. This SOF thread might help you http://stackoverflow.com/questions/6979612/why-constructors-will-always-have-same-name-as-of-class-and-how-they-are-invoked
Aditya Puri
1,080 PointsWhat do you mean by "keyword" ?
yadin michaeli
Courses Plus Student 5,423 PointsHello Aditya
maby i will try to help here.
when you create a new object you type PezDispenser dispenser = new PezDispenser ()
in the parentheses you type the Constructors that you wrote in PezDispenser class
the Constructors is this public PezDispenser(String characterName) {
mCharacterName = characterName;
}
when you type this line of code
you can give in PezDispenser dispenser = new PezDispenser () a new character name that you like between the two parentheses beacuse you initialize mCharacterName field and give the Constructors a String parameters hope it help :)
Aditya Puri
1,080 PointsAditya Puri
1,080 Pointsbut still...how is the constructor a method? It doesn't behave like one...it makes a new class...it doesn't return anything or do any calculation. And I don't think you can do this in your code-
Method method = new Method("Something");