25.5 Declaring and Using Classes
[Note: This section may be read after studying Chapter 7, Introduction to Classes and Objects.]
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. 7.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. 7.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 ...
Get Java How To Program, Late 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.