25.5 Declaring and Using Classes
[Note: This section may be read after studying Chapter 3, Introduction to Classes, Objects, Methods and Strings.]
In Section 25.3, we demonstrated basic JShell capabilities. In this section, we create a class and manipulate an object of that class. We’ll use the version of class Account
presented in Fig. 3.1.
25.5.1 Creating a Class in JShell
Start a new JShell session (or /reset
the current one), then declare class Account
—we ignored the comments from Fig. 3.1:
jshell> public class Account {
…> private String name;
…>
…> public void setName(String name) {
…> this.name = name;
…> }
…>
…> public String getName() {
…> return name;
…> }
…> }
| created class Account
jshell>
JShell recognizes when you enter the ...
Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.