From the course: Java 8 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Storing data in instance variables

Storing data in instance variables - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Storing data in instance variables

- 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…

Contents