This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We have a new object in our project; let's see how to use it in our existing code!
Related Links
Things to Consider
- DRY (Don't Repeat Yourself) and other tips from The Pragmatic Programmer
-
SOLID Principles of Object-Oriented Programming:
- S: Single Responsibility Principle
- O: Open/Closed Principle
- L: Liskov Substitution Principle
- I: Interface Segregation Principle
- D: Dependency Inversion Principle
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's go back to fun facts activity and
use this new method.
0:00
First, we need to instantiate
a fact book object.
0:04
We'll do it similarly to how we created
a new OnClickListener down here or
0:08
a new random object in
the fact book class.
0:12
We'll use this new fact book object to
do the work of getting us a random fact
0:15
when the button is clicked.
0:19
We could create it here,
in the OnClick method.
0:21
But if we do that,
0:24
a new fact book object would be created
every time we tap on the button.
0:25
That doesn't seem very efficient.
0:30
What if our object was huge,
with thousands of facts to choose from?
0:32
On each tap,
0:36
we'd be wasting tons of processing time,
just in recreating the fact book.
0:37
Instead, let's create it just once,
as a field, or member variable.
0:41
Up here before the onCreate method,
but after we define our class,
0:46
let's add some space, and
type private FactBook, with a capital F.
0:51
And then let's name it factBook.
0:58
Which if we type a lowercase f,
Android Studio will recommend for
1:00
us, and finally, a semicolon.
1:04
Notice we're using the class we
just created as the data type for
1:08
this variable.
1:12
We're also using the private keyword
because we only want this variable
1:14
to be available inside this class.
1:18
No other codes outside our
activities need to know about it.
1:22
Next we need to initialize it.
1:25
Before the semi colon
type equals new F and
1:27
use the auto complete to finish it.
1:32
Auto complete even adds
the parenthesis to the end.
1:37
This creates a new FactBook object using
the default constructor, but wait, we
1:40
didn't have a constructor to our FactBook
class, and luckily we don't need to.
1:45
If we don't provide our own constructor,
1:50
Java will automatically
create one behind the scenes.
1:52
So even if we don't add
a constructor to our class,
1:56
we can always create new objects from that
class by using the default constructor.
1:59
New, then the class name, and
then an empty set of parentheses.
2:04
Back in the on-click method, lets get a
random fact from our fact book object and
2:08
store it in a variable named fact.
2:14
Type String, fact, and
2:16
set it equal to factBook.cool.
2:19
Our get fact method is right
at the top of auto complete.
2:24
But where did the rest of these methods
come from, the ones we didn't write?
2:27
In java every single class either directly
or indirectly extends the object class.
2:32
Even though we didn't use the extends
keyword when writing our FactBook class,
2:38
it still extends the object
class by default.
2:42
So these other methods come
from the object class.
2:45
Let's hit Enter,
then add a semicolon, and we're done.
2:49
Now let's take a quick review of our code.
2:54
We create our activity and give it
the layout we'd like it to display.
2:57
Then we initialize our view
variables to the views in the layout
3:01
by using their ids.
3:05
Then we create an onClickListener for
our button and when the button is tapped
3:07
we use our FactBook object to get a random
fact and update the factTextView.
3:11
Finally, we attach our on
click listener to our button.
3:16
This looks pretty good, but let's take
one more pass at our fact book class.
3:19
First, we see one of the main
problems with using comments.
3:25
We ideally what to write a code
that is easy to understand with
3:29
out additional comments.
3:32
Comments tend to be forgotten about
after they are first written and
3:34
can end up not making any sense.
3:37
Like this one, let's delete it.
3:39
All right, there's one more
thing we should change here.
3:44
Can you guess what it is?
3:47
One of the properties of our fact
book is all of the facts it contains.
3:48
So this facts array should
really be a property, field or
3:52
member variable of our fact book and not
a local variable of the getFact method.
3:55
Let's cut the facts declaration from the
getFact method, And then paste it up here.
4:00
Next we're going to test the app.
4:15
But first, just for fun, I'm going to show
you another way we could have done this.
4:17
You don't have to follow along with this
part, but you're more than welcome to.
4:22
I'm going to use Cmd or
Ctrl+Z to undo all the way back to here.
4:26
Then I'll right-click on the facts
variable, and select refactor.
4:30
But instead of picking rename I'm going
to choose extract, and then field.
4:39
Then I'll choose where to initialize this
new field, I'll pick field declaration,
4:46
which will put it at the top of the class,
and then hit enter.
4:52
And then we go,
4:58
Android studio automatically factored
our local variable to a field.
5:00
It even deduced that our facts
variable can be declared as final
5:04
because we never make any changes to it.
5:08
Its functionality equivalent
to what we had before.
5:10
But just to make it look exactly the same
I'll delete the final keyword and
5:13
this optional new string bit.
5:17
And finally I'll cut and paste our
comment to be above our new field.
5:26
There are tons of helpful tricks
like this in Android Studio.
5:35
And they get even faster when you
start using the keyboard shortcuts.
5:38
Last but not least, we're getting
a couple of warnings from Android Studio.
5:42
Each of them is just letting us know that
we can use a stricter access modifier.
5:47
And luckily, we can fix both of
these just by using Alt+Enter.
5:51
But enough about that,
we'll have more time for tips and
6:02
tricks once we get the basics down.
6:04
For now, let's run our app to test it.
6:07
Remember, the goal of refactoring
is to make changes to the code
6:13
without changing the behavior of the app.
6:16
So it should work exactly
the same as before.
6:19
Great work.
6:27
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up