Join David Gassner for an in-depth discussion in this video Storing data in instance variables, part of Java 8 Essential Training.
- When you create a custom class, you can design it to encapsulate data using InstanceVariables or fields. I have a main class, and I'm going to create a new class in a new package that I'll name model. I'll right-click on my base class, com.example.java and choose new package. And I'll name the new sub package, model. I'm naming it this because this is a common bit of Java vocabulary. A model class defines your data model.
Now I'll right-click on that new package and create a new Java class and I'll name it Olive. My goal is to create my encapsulated Olive press system. Each instance of my Olive class will represent a single piece of fruit, a single olive. And so I'll need to represent its name, its color and the amount of oil it would yield when crushed in an olive press. I'll define these values as fields of the class.
I'll start with a public string and I'll give it a variable name of simply, name. Then I'll create another public string and I'll give this one a type of long and give it a name of color. And finally, I'll define a public integer and I'll name that one oil. Now for this first version of the application, each instance of the Olive class will have the same values. So I'm going to give this one a name of Kalamata.
That's one kind of olive. And I'll give it a color of 0x2E0854. That's indigo, which is a dark purple and approximates the color of a Kalamata olive. And I'll set the amount of oil that I get from a single olive of the Kalamata variety at a value of 3. So now my class is ready to use. I'll just get rid of this comment that was generated and I'll come back to my Main class.
Within my Main class, I'll create an array list that will contain all of the olives I want to crush. I'll declare an instance of the List interface and then I'll set the type of the members of the list as Olive. Notice when I select Olive that an import statement is added for that class because it's in a different package. I'll name my list olives and I'll instantiate it with new ArrayList. Just as before, I don't need to redeclare the type of the objects that are referenced in the list.
Next I'll declare one of the olives. I'll set the data type as Olive, and I'll name it olive1. And I'll instantiate it with new Olive. As I described previously, this class does not have an explicit constructor method, but that's fine because the Java compiler creates it for me. Then I'll add the olive to the list with olives.add olive1. Now I'll select and duplicate this line of code three times, and then I'll change the names of the olive variables to olive2 and 3, and I'll them here as well.
Now notice, I declared all three of these instance variables or fields as public so I can set and get their values without any other special code. So next, I'll add a for loop and I'll loop through the collection of olives. I'll declare a variable, data type as olive, and I'll just give it a name of o. And I'll iterate through the olives collection. Then I'll output the name of the olive, with o.name. And the result is that I have 3 Kalamata olives in my collection.
Now I'm also going to create one more class before I'm done with this step of the process. And this will be my olive press class. I'll place this in my base package. I'll create another new Java class, and I'll name it OlivePress, and then I'll create an instance method, a method that's a member of the OlivePress class. This will be a public method, so it can be called from anywhere in the application, and it will return an integer. Its name will be getOil and it'll receive an argument, a list of olives.
If you see this message indicating that the list interface isn't recognized, place the cursor back on that line and press ALT or Option and the Enter key and that should add the required import statement. Now for the moment, I'm just going to return any integer value, just to make sure that the code is working. Then I'll come back to my Main class and I'll take this for loop and I'll cut it from the code. I'll move back to the OlivePress class, and I'll paste the code right before the return statement.
I'll come back to the Main class. Now I'll create an instance of the OlivePress class. I'll declare its type and I'll name this one press, and I'll instantiate it with new OlivePress. Then I'll call press.getOil and I'll pass in my olives collection. So now, the Olive class contains the string, the name of the olive. The OlivePress class has a method that can process the data, and the main class is creating the data and passing it to the OlivePress for processing.
I'll run the code and the result looks the same, but now I've separated the code out. I have a Model class, Olive, to contain and manage the data directly; and an OlivePress class to do the processing. In the next steps, I'll fine tune my code a little bit more, following some best practices of Java development.
- 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