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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsMy Review - Java Classes
Hi all
As some of you may know I've been struggling to understand some of these Java Concepts so I've been having another go starting with a review of classes based on what's in this video. Writing the code for the video in my IDE and making relavant notes.
https://teamtreehouse.com/library/java-data-structures/getting-there/class-review
The codee works but I wanted to see if I got my understanding right based on my comments and notes? Am I getting there? :)
Class Review
Create a file the main java file with the command line boilerplate and another that is a public class called Treet. They're shared with each other in the package. as long as they're both within the package.
Treet has member variables that are declared within the class. They use an "m" naming based convention so we can differentiate them when we initialise them.
Define a constructor function to initialise the member variables each with an object type defined and variables passed in as parameters.
Use "getter" functions to return values to the different objects.
Instiantiate - that is create a new instance of the Treet class in Main.java.
Treet.java
//use package
package com.teamtreehouse;
//import packages
import java.util.Date;
//create a new class of Treet;
public class Treet {
//create new member variables that stores data. these are immutable
private String mAuthor;
private String mDescription;
private Date mCreationDate;
public Treet(String author, String description, Date creationDate) {
mAuthor = author;
mDescription = description;
mCreationDate = creationDate;
}
}
/*They need to be set with "getter functions". They return different objects from the constructor.*/
public String getAuthor() {
return mAuthor;
}
etc
New file to instiantiate a Treet object. A new instance of the constructor.
public class Example {
public static void main(String[] args) {
Treet treet = new Treet(
"This is the author username mName",
"This is the description. mDesription.",
new Date(1421849732000L)
);
//
System.out.printf("This is a new Treet: %s %n, treet);
}
}
2 Answers
Jennifer Nordell
Treehouse TeacherHiya! I think your understanding is about the same as mine (at least as far as I understand it). However, one thing I would note is this: the getter/setter functions are getting/setting the properties from/to the object because they're private and not accessible outside the class.
I have a friend who is actually a Java developer and they suggested I read this book. "Design Patterns Explained" by Alan Shalloway and James R. Trott. The authors contend that students who learn design patterns at the same time they're learning OOP tend to learn OOP a little faster (and better) than others. I'm not done reading it yet, but it seems like my grasp on OOP is much better now than it was when I started. And I come from a background in procedural programming
But I definitely think you're on the right track!
Jonathan Grieve
Treehouse Moderator 91,253 PointsHi Jennifer :)
The setting part I probably need to look into a little more as I don't think I've seen these used too much in practice. But at a guess I'd say set values to the variables that are then returned by the getters? Hmmm I don't know now something sounds off about that :)
So is this why we have getters. To make values that are immutable, their values can't be changed, available to the class again so we can do things with them?
Thanks :)
Jennifer Nordell
Treehouse TeacherNow, I don't generally just randomly recommend that someone skip over to a different language. But sometimes a fresh perspective can cause a sort of "light bulb" moment
As you probably know, C# is also an OOP programming language with strong roots in C (just as Java is). So with that in mind, I can recommend these videos here at Treehouse:
Encapsulation: https://teamtreehouse.com/library/c-objects/encapsulation-and-arrays/encapsulation
Accessor Methods (getters/setters): https://teamtreehouse.com/library/c-objects/encapsulation-with-properties/accessor-methods
Just listen and look. No need to code. Try and see if you can understand the point he's trying to get across
Jonathan Grieve
Treehouse Moderator 91,253 PointsI guess I'll have a look. :) My hesitation is sad I know but I don't feel ready to look at the videos until I'm looking to finish the course itself.
I'll have a look at the topics though because you're right another perspective can be a good thing. :)