From the course: Java 11+ Essential Training

Unlock the full course today

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

Create instance fields and methods

Create instance fields and methods - Java Tutorial

From the course: Java 11+ Essential Training

Start my 1-month free trial

Create instance fields and methods

- [Instructor] Custom classes can contain data and implement functionality that let you manage application state. When you store variables as members of class instances, they're called instance fields. Java developers typically declare these bits of data as private to their classes, and then provide access to the data through methods that are also members of the classes and these are known as instance methods. To demonstrate this, I'll create a new package that I'll name model. And then I'll create a new Java class within that package that I'll name clothing item. Each instance of the clothing item class will have an item type, a size, a price, and a quantity. I'll declare these as private fields. I'll start with the keyword private. And then set the type of the variable and its identifier. I won't set a default value. I'll do the same thing for the size. And now I'll set the price. This will be a double value, a…

Contents