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 - Java Tutorial
From the course: Java 8 Essential Training
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
-
-
(Locked)
Understanding encapsulation7m 19s
-
Creating and instantiating custom classes4m 51s
-
(Locked)
Creating nested and anonymous classes4m 17s
-
(Locked)
Visualizing class relationships3m 43s
-
(Locked)
Storing data in instance variables6m 2s
-
(Locked)
Declaring instance and static methods3m 59s
-
(Locked)
Using constructor methods4m 56s
-
(Locked)
Using static variables as constants3m 3s
-
(Locked)
Declaring and using enum types5m 17s
-
(Locked)
-
-
-
-