From the course: Java 8+ Essential Training: Objects and APIs
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Create and use instance methods - Java Tutorial
From the course: Java 8+ Essential Training: Objects and APIs
Create and use instance methods
- [Instructor] I've previous described how to use static methods that are called from a class without the class first being instantiated. In this code I have a method named getInput that's defined in the class InputHelper. I call the method from the class itself. I'll take a look at that method by control clicking Windows or command clicking on Mac, and I see that the method has the keyword static, and that's what allows the method to be called in this way, but frequently a method needs to work with persistent data that's stored within an object, then it's known as an instance method, that is a method that belongs to an instance of the class rather than to the class itself. Static methods are marked with the keyword static. Instance methods simply don't have that keyword. So if I delete the keyword static, this method now must be called from an instance of the InputHelper class, not from the class. I'll go back to the main class, and I see that there's now an error indicated, and it's…
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
-
-
-
-
-
-