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
The `@Deprecated` annotation allows us to mark methods, classes, and fields for future deletion, giving other developers a compiler message to update their code. In this video we'll see how the `@Deprecated` annotation provides a way for us to be responsible and courteous developers.
Onto our next built-in annotation,
deprecated.
0:00
This annotation is placed on methods or
classes that are still functional but
0:04
will be phased out in a future release.
0:07
So, developers should update their
code base as soon as is reasonable.
0:09
If at all possible,
0:14
we don't want to break existing code that
is using a method we want to deprecate.
0:15
Let's take a look at an example to
illustrate how using the deprecated
0:20
annotation can make us
courteous developers.
0:24
In the deprecated package of
the same workspace's project,
0:28
consider the Seller class.
0:31
It has a single method in there called,
placeAdInNewspaper().
0:34
Upon updating our class for
the next release, we decide we wanna
0:38
phase out this method and provide a new
method called postInCraigslist().
0:42
Adding the new method is
pretty straightforward.
0:47
Let's create that now.
0:49
I'll make this one public void as well,
postInCraigslist().
0:51
I'll add a couple parameters,
just for demonstration purposes.
0:58
We'll get the text for the ad in there,
1:02
as well as a list of images that we might
want to include with our Craigslist post.
1:04
And then here,
we'll do all of the new school stuff.
1:11
Since I've newly introduced the list and
image classes to this file,
1:18
I'll add those import statements now.
1:22
For the list, that's java.util.list.
1:26
And for image, that's java.awt.image.
1:29
You might think that if we are going
to get rid of the old method anyway,
1:37
we should just delete it now.
1:40
But, if we do that, then another developer
who is using our updated library,
1:43
might find herself scrambling
because her code no longer compiles.
1:48
I can demonstrate that now.
1:52
Down in my console,
I'll switch to the source directory.
1:53
And I will compile with the javac command.
1:56
And when I do that,
2:05
you can see that an error has
indeed occurred in the main class.
2:06
And this is because right here, I have
called the method placeAdInNewspaper().
2:11
A better solution,
would be to communicate to
2:18
other developers that the newspaper
method is going to be phased out.
2:21
To do this, we can add
the deprecated annotation to it.
2:25
This will force the compiler
to generate a warning.
2:29
The nice thing about this being a warning,
is that it won't prevent our code from
2:32
compiling, but
it will give us an informative message.
2:36
[SOUND] Another cool thing is that most
IDEs will give you a visual indication of
2:39
this wherever you try to
call a deprecated method,
2:44
usually by crossing out the method call.
2:46
This is a good heads up to other
developers that their code should be
2:49
updated, because a future release
will no longer have this method.
2:52
Kind of like your highschool teacher,
crossing out a phrase on your research
2:56
paper, indicating that you need
to use alternative wording.
2:59
Instead of deleting the message,
which I will undo now,
3:04
let's add the deprecated annotation to it.
3:09
And now, when we recompile,
we get a nice heads up from the compiler.
3:14
Indeed, this is much nicer than a broken
application that does not compile.
3:21
It gives developers a chance to update
their code to use the preferred methods.
3:26
You need to sign up for Treehouse in order to download course files.
Sign up