1 00:00:00,520 --> 00:00:04,000 Onto our next built-in annotation, deprecated. 2 00:00:04,000 --> 00:00:07,900 This annotation is placed on methods or classes that are still functional but 3 00:00:07,900 --> 00:00:09,780 will be phased out in a future release. 4 00:00:09,780 --> 00:00:14,420 So, developers should update their code base as soon as is reasonable. 5 00:00:14,420 --> 00:00:15,680 If at all possible, 6 00:00:15,680 --> 00:00:20,780 we don't want to break existing code that is using a method we want to deprecate. 7 00:00:20,780 --> 00:00:24,160 Let's take a look at an example to illustrate how using the deprecated 8 00:00:24,160 --> 00:00:26,630 annotation can make us courteous developers. 9 00:00:28,690 --> 00:00:31,590 In the deprecated package of the same workspace's project, 10 00:00:31,590 --> 00:00:34,090 consider the Seller class. 11 00:00:34,090 --> 00:00:37,455 It has a single method in there called, placeAdInNewspaper(). 12 00:00:38,960 --> 00:00:42,260 Upon updating our class for the next release, we decide we wanna 13 00:00:42,260 --> 00:00:47,220 phase out this method and provide a new method called postInCraigslist(). 14 00:00:47,220 --> 00:00:49,390 Adding the new method is pretty straightforward. 15 00:00:49,390 --> 00:00:51,270 Let's create that now. 16 00:00:51,270 --> 00:00:58,300 I'll make this one public void as well, postInCraigslist(). 17 00:00:58,300 --> 00:01:02,450 I'll add a couple parameters, just for demonstration purposes. 18 00:01:02,450 --> 00:01:04,180 We'll get the text for the ad in there, 19 00:01:04,180 --> 00:01:08,520 as well as a list of images that we might want to include with our Craigslist post. 20 00:01:11,230 --> 00:01:15,990 And then here, we'll do all of the new school stuff. 21 00:01:18,961 --> 00:01:22,350 Since I've newly introduced the list and image classes to this file, 22 00:01:22,350 --> 00:01:24,310 I'll add those import statements now. 23 00:01:26,110 --> 00:01:29,970 For the list, that's java.util.list. 24 00:01:29,970 --> 00:01:34,924 And for image, that's java.awt.image. 25 00:01:37,060 --> 00:01:40,060 You might think that if we are going to get rid of the old method anyway, 26 00:01:40,060 --> 00:01:41,770 we should just delete it now. 27 00:01:43,770 --> 00:01:48,310 But, if we do that, then another developer who is using our updated library, 28 00:01:48,310 --> 00:01:52,320 might find herself scrambling because her code no longer compiles. 29 00:01:52,320 --> 00:01:53,890 I can demonstrate that now. 30 00:01:53,890 --> 00:01:56,980 Down in my console, I'll switch to the source directory. 31 00:01:56,980 --> 00:01:59,454 And I will compile with the javac command. 32 00:02:05,156 --> 00:02:06,580 And when I do that, 33 00:02:06,580 --> 00:02:11,390 you can see that an error has indeed occurred in the main class. 34 00:02:11,390 --> 00:02:16,855 And this is because right here, I have called the method placeAdInNewspaper(). 35 00:02:18,500 --> 00:02:21,510 A better solution, would be to communicate to 36 00:02:21,510 --> 00:02:25,800 other developers that the newspaper method is going to be phased out. 37 00:02:25,800 --> 00:02:29,100 To do this, we can add the deprecated annotation to it. 38 00:02:29,100 --> 00:02:32,420 This will force the compiler to generate a warning. 39 00:02:32,420 --> 00:02:36,687 The nice thing about this being a warning, is that it won't prevent our code from 40 00:02:36,687 --> 00:02:39,721 compiling, but it will give us an informative message. 41 00:02:39,721 --> 00:02:44,031 [SOUND] Another cool thing is that most IDEs will give you a visual indication of 42 00:02:44,031 --> 00:02:46,882 this wherever you try to call a deprecated method, 43 00:02:46,882 --> 00:02:49,223 usually by crossing out the method call. 44 00:02:49,223 --> 00:02:52,457 This is a good heads up to other developers that their code should be 45 00:02:52,457 --> 00:02:56,660 updated, because a future release will no longer have this method. 46 00:02:56,660 --> 00:02:59,840 Kind of like your highschool teacher, crossing out a phrase on your research 47 00:02:59,840 --> 00:03:04,090 paper, indicating that you need to use alternative wording. 48 00:03:04,090 --> 00:03:07,330 Instead of deleting the message, which I will undo now, 49 00:03:09,010 --> 00:03:11,480 let's add the deprecated annotation to it. 50 00:03:14,810 --> 00:03:18,590 And now, when we recompile, we get a nice heads up from the compiler. 51 00:03:21,090 --> 00:03:26,690 Indeed, this is much nicer than a broken application that does not compile. 52 00:03:26,690 --> 00:03:31,210 It gives developers a chance to update their code to use the preferred methods.