From the course: Java 8+ Essential Training: Objects and APIs
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Manage state with instance variables - Java Tutorial
From the course: Java 8+ Essential Training: Objects and APIs
Manage state with instance variables
- [Instructor] Just as with instance methods that belong to an instance of a class, there are also instance variables. A variable that's declared within a method is called a local variable. It's local to that method. So for example in this class InputHelper, I have a variable called scanner. It's an object, it's declared and used within the method and its reference expires when the method is done. In a non-static class, when you declare a variable that's in the class but outside of any methods, that's an instance variable. Like the method, it won't have the keyword static. Now, in this project, I've returned the InputHelper to its static state. That is the getInput method is static and can be called directly from the InputHelper class. And I have a starting Main method here. I'm going to create a new class in my utilities package that I'll call MathHelper. Within the MathHelper class, I'll declare a private variable starting with the keyword private, then the type, which will be int…
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
-
-
-
-
About encapsulation3m 46s
-
Use the Java runtime classes4m 45s
-
Wrap code in static methods5m 56s
-
Declare and use custom classes4m 9s
-
Organize code with packages3m 46s
-
Create and use instance methods2m 27s
-
Manage state with instance variables7m 31s
-
Declare multiple constructor methods5m 2s
-
Use static fields as constants5m 51s
-
Declare and use enum types3m 44s
-
Organize code with nested types4m 15s
-
-
-
-
-
-