Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build an Interactive Story App (Retired) The Model-View-Controller Pattern Adding Custom Constructors

Ragesh Kanagaraj
Ragesh Kanagaraj
3,637 Points

I am completely confused with this customer constructor concepts in this current tutorial

Can anyone explain me this concept of custom constructor and why are we using it ? I have decent knowledge on java. But this tutorial seems to be very confusing for me and I am not able to understand why we are creating classes for story page choice and calling the constructors.

1 Answer

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Ragesh,

There are several reasons why one might add custom constructors to a class. In this specific example, custom constructors are used to make the model simple and easy to construct.

So think of a class as a blueprint for a house. The class has the design layout for what an object instance will look like when constructed. Every class has a default constructor that has no parameters. This default constructor is included by Java on your behalf so you don't have to create a parameterless constructor for every class you create.

Using a custom constructor allows you to construct the class with the default values you want to supply. So now imagine what it would be like to create and setup the objects without the convenience of the custom constructors that were created in the video. You would have to instantiate the object with the default constructor and then call several methods immediately following the creation of the object like this.

private void MethodA() {
    ClassA classA = new ClassA();
    classA.setProperty1("value 1");
    classA.setProperty2(true);
    classA.setProperty3(6);
    classA.setProperty4("value 4");
    ...

    ClassB classB = new ClassB();
    classB.setProperty1("value 1");
    classB.setProperty2("value 2");
    classB.setProperty3(true);
    classB.setProperty4(23);
   ...
}

Not very elegant, in my opinion. If we add custom constructors to ClassA and ClassB we'll be able to accept values to be set on member variables during the construction of the object. It would look like this.

private void MethodA() {
    ClassA classA = new ClassA("value 1", true, 6, "value 4");
    ClassB classB = new ClassB("value 1", "value 2", true, 23);
}

A lot nicer to work with, right? Our API is now simpler and easier to use. The custom constructor simply allows you to define what the defaults are instead of the class making assumptions and depending on the user of the class to call all the right set methods to properly configure the class as needed.

I hope this helps.