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.
Ashwin Kumar Kurella9,984 Points
Why should we use a private constructor?
Can't we use some other method which takes a Parcel object as a parameter, to read the contents of a parcel, instead of the private constructor?
Assuming you mean an instance method inside the Day class, it could certainly take a Parcel as a parameter like you said. But where would the Parcel data be assigned? And how would that method even be invoked in the first place? To invoke it we'd have to already have an instance of Day, and that means we'd probably have already set all of its fields!
We want to make a new instance of Day using data from the Parcel object, so that's why it's in a constructor, not an instance method. Why private? Because generally you want to strive to give the least amount of access necessary to any data within the code you write. In the Day class, it looks like Ben only intended the static Creator<Day> object instance to use that constructor, so he limited the constructor's access to private. He could've made it public, but that would raise the questions: where outside the Day class is it being used, and why does it need to be used there? At this point in the project it didn't need to be used anywhere else, so it wasn't enabled to be used elsewhere.
J.D. Sandifer18,813 Points
In the video, Ben explained that "This is all a pattern that the Android system understands and expects to be in place. It may seem like a lot of work, but it makes our data available to use wherever Android uses parcelable data."
Sometimes the quick explanations like that can get lost among the sea of information contained in the videos - it's definitely happened to me a few times.
Hopefully that helps!