Join David Gassner for an in-depth discussion in this video Declaring instance and static methods, part of Java 8 Essential Training.
- As I've previously described, when you create a method that's a member of a class, and you don't include the static key word, that means it's an instance method. A class that's designed to encapsulate data, such as my model class, named Olive, typically will have instance methods known as setters and getters. The getter is called the accesor method, and the setter is know as the modifier method. If your class already has instance variables, your IDE typically will be able to generate the setters and getters for you.
And IntelliJ IDEA does this well. I'll make a bit of space below the variable declarations, then I'll right click and choose Generate. And from the menu that appears I'll chose getter and setter. Then I'll chose all three instance variables, and click okay. And that generates public methods. They all start with the words get or set, and finish with the variable names. Getters simply return the value, and setters receive an argument.
Notice within a setter method, the key word this is used to distinguish the variable that's a member of the instance from the argument, which has the same name. So I'm taking the argument, and that's the reference to it, and I'm assigning it to the instance variable. Now just for fun, I'm going to change the name of this method, get oil, to crush, because it's going to return the amount of oil that this particular olive yields. And then, so I can track what's going on, I'll add some output code, and I'll output the word ouch, which is what an olive says when you crush it.
Now, I'll come back to the olive press class. Within the olive press class, I'll declare an integer value, and I'll name it total oil. I'll initialize it to a value of zero. Then, within the loop for each olive, I'll extract the oil from the olive by calling the crush method. I'll say total oil+=o.crush, so that's going to return the value, and it's going to output the string that I commanded it to. Then, I'll change the return statement so it's returning the total oil.
I'll come back to my main class. Within the main class, I'll declare an integer variable, and I'll assign it the value from the call to the get oil method. Then, I'll report the total olive oil that was extracted, with Total olive oil + total oil. For each olive, I'm outputting the name of the olive, and I see the message ouch as the olive is crushed, and then at the end of the process I see that the total olive oil is nine. But let's make some changes.
Let's say that we have different sizes of olives. So for each of the olives, I'm going to set the explicit amount of oil. Here I'll say olive2.setoil and I'll give it a value of one. That's a really small olive. And then for this one, I'll call olive3 and again I'll call setOil, and I'll pass in a value of four. That's a slightly larger olive than the norm. And I'll run the code again. And this time my total olive oil is a little bit less.
Eight. So now all the work is being done by the olive class, which tells the rest of the world how much oil the olive has, by the olive press that knows how to process the oil, and the olives are actually being created, and given their oil values, by the main method. We're using instance methods and instance variables to manage the data. To fully encapsulate this, and make sure no other part of the application can get to this data without going through the methods, I'll change all three of these instance variables to private.
And now, the only access to them is through the getters and the setters.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files