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