YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Creating a Static Method - Intro to Java Programming

Get Embed Code
4 Languages

Showing Revision 6 created 08/31/2017 by Dejan Trajkovic.

  1. So, let's try this. I would put all of this functionality in a method, give me
  2. a minute to arrange that and then I'll show you. As promised, here is my method.
  3. This method keeps reading an integer until it finds
  4. one that is within the desired range and then it
  5. returns it. The exact code isn't really important right
  6. now. You can have a look at it in the
  7. code distribution, if you want to. And, out here,
  8. I'm calling it. But, there's a problem. When I compile
  9. it, I get an error message. And, that error
  10. message, right now, must truly appear like gibberish to you.
  11. It talks about non-static methods and static methods.
  12. So let me explain what's happening and why the
  13. compiler is unhappy. Normally, we call a method on
  14. an object and I didn't do that here because
  15. I figured, well I'm in elevator demo. So I
  16. should be able to call any elevator demo method
  17. as well. But the trouble is I'm calling from
  18. main. So there are no elevator demo objects and
  19. the compiler complains. So going back here, that is
  20. the crux of the problem. ReadBetween is not called
  21. on an object And there really isn't a great
  22. object to call it on. it would have been nice
  23. to call it on the scanner, but like I
  24. already said, we can't do that. Whenever you're in this
  25. kind of situation where you have a method that
  26. can't really be called in an object, then you make
  27. a static method. The static method is a method that's not called in any
  28. object. So, one remedy is, simply, to go back into elevator
  29. demo. And, declare the helper method static. Like this. Now, all
  30. will be well. And the syntax error is gone. So to answer our original question,
  31. it is perfectly legitimate to put read between
  32. intra ElevatorDemo as a static method but maybe
  33. it's not optimal. Because what if I want
  34. to use that perfectly nifty method in another
  35. class? So really, read between is a method that seems it would be useful in any
  36. situation that you want to read a value from
  37. some range. So we, we might really want to put
  38. this somewhere else, and the class that I'm going
  39. to put it on I'll call IO Helper. I've
  40. prepared that class for you, let me show you.
  41. Here is the IO Helper class. Here is the
  42. read between method. And again, that is a static
  43. method because I'm not going to actually make any
  44. objects off the IO Helper class. I just want
  45. it to be the home for this method. That means
  46. when I call the method, I have to specify
  47. the name of the class like this. Here you
  48. see the name of the class, IOHelper, the name
  49. of the method. Static methods are not incredibly common because,
  50. like I said,most of the time, an actual object
  51. is responsible for carrying out a method, but they do
  52. happen. In fact, you've seen a few of them. In
  53. lesson four, you saw a good number of static methods
  54. in the math class. Again, the syntax is the name
  55. of the class, a period, and then the name of
  56. the method. And once again there is no object of
  57. the math class. We simply call the square root method
  58. directly on the class. Now, you may wonder, why
  59. did we have to use a static method? Why
  60. couldn't we just call the method on the number
  61. 2? Well, that's what it's like in Java. In
  62. Java, numbers are not objects so you can never
  63. call a method on an object. A call such
  64. as 2. square root might be more logical, but
  65. it's an error. So to summarize the static method.
  66. Belongs to a class. It's not called on an object and they're not actually all
  67. that common. Why do you call them
  68. static methods? There's no good reason actually. That's
  69. a holdover from another much more ancient
  70. programming language. There's nothing particularly static about them.
  71. In addition to static methods, there are static
  72. variables. Let's have a look at them next.