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
Steve Jones
Java Web Development Techdegree Student 78 PointsJava Constructor and Data Hiding, is all about the important part?
As I am a beginner, so far, I understood we have to use Constructor to initialize every new objects without writing manually and for Data Hiding (Encapsulation), we have to protect our class data variables and methods from unknown changes directly to that particular class. Kindly correct me, if I am wrong?
Is this what those two functions is essential for every project, I mean I have to use constructor as well as getter setter for my projects?
1 Answer
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsYour quote looks correct to me:
As I am a beginner, so far, I understood we have to use Constructor to initialize every new objects without writing manually and for Data Hiding (Encapsulation), we have to protect our class data variables and methods from unknown changes directly to that particular class.
About the question. I don't really understand what you are trying to ask ... May be you could re-phrase
Is this what those two functions is essential for every project, I mean I have to use constructor as well as getter setter for my projects
I'd say Constructor and getters/setters are essential to classes, not projects (don't really understand what do you mean by that) you create in Java.
Some classes can be used without constructors, if they have static methods like Integer.parseInt can be used without constructing new Integer class...
And you can sometimes if you need to create classes without getters/setters, in more advanced situations, when you get to 'Spring with Hibernate' course, or 'Intro to Spark' there will be examples...
Most of the time, Constructor+getters/setters is important to your "Model"/"Domain"/"Entity" classes, like Team, Project, Role, User... Classes that will have properties, and can be saved to databases...
So overall it is very important to look at every project independently ...
And there decide which class will look like. As I said, there can be classes without constructors, although I don't think that you will ever write those, although may be you could.
And can be classes without getters/setters.
I would say, If you decide, and go through treehouse 'Java' Track, or better 'Java Web development' Track you'll see a lot of examples of classes made by Craig and Chris, and will be able to see how classes should look like. Because only through concrete examples on concrete project will you be able to grasp the concept how and which class can be constructed
Steve Jones
Java Web Development Techdegree Student 78 PointsSteve Jones
Java Web Development Techdegree Student 78 PointsThanks for the brief reply. Now I understood :D