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.

Declare and initialize object variables

Declare and initialize object variables - Java Tutorial

From the course: Java 11+ Essential Training

Start my 1-month free trial

Declare and initialize object variables

- [Narrator] If a variable's data type isn't a primitive, then it's an object of some kind. When you build Java applications, you're creating classes. A class is essentially a template for creating objects. So for example, if I wanted to create a pattern where I could store information about types of clothing, I might create a class called clothing item. That clothing item class could have fields for data values and methods that knew how to do stuff. When you create an object variable, you're creating an instance of one of those classes. And because the classes can define fields for data values, and methods for actions, when you create the object that's an instance of the class, it has those fields and methods. In Java, a field is a variable that's a member of a class. A local variable is a variable that you declare inside of a method, while a field is a variable that's declared after the class declaration. Now we'll talk…

Contents