classEmployee{...> &am"/> classEmployee{...> &am">

Home  >  Article  >  Java  >  How to implement encapsulation concept in JShell in Java 9?

How to implement encapsulation concept in JShell in Java 9?

WBOY
WBOYforward
2023-09-02 22:01:02808browse

如何在Java 9的JShell中实现封装概念?

Java Shell (JShell for short) is a REPL interactive tool for learning Java and prototyping Java code. It evaluates the entered statements, statements, and expressions and immediately prints out the results and runs them from the command line.

Encapsulation is an important concept in Java, used to ensure that "sensitive" data is hidden from users. To achieve this, we have to declare the class variables as private and provide public access to the get and set methods as well as update the value of the private variable.

In the following code snippet, we have implemented the concept of encapsulation for the Employee class.

<strong>jshell> class Employee {
...>       private String firstName;
...>       private String lastName;
...>       private String designation;
...>       private String location;
...>       public Employee(String firstName, String lastName, String designation, String location) {
...>          this.firstName = firstName;
...>          this.lastName = lastName;
...>          this.designation = designation;
...>          this.location = location;
...>       }
...>      public String getFirstName() {
...>         return firstName;
...>      }
...>      public String getLastName() {
...>         return lastName;
...>      }
...>      public String getJobDesignation() {
...>         return designation;
...>      }
...>      public String getLocation() {
...>         return location;
...>      }
...>      public String toString() {
...>         return "Name = " + firstName + ", " + lastName + " | " +
...>                "Job designation = " + designation + " | " +
...>                "location = " + location + ".";
...>      }
...> }
| created class Employee</strong>

In the following code snippet, we create an instance of the Employee class and print out the name, designation and location.

<strong>jshell> Employee emp = new Employee("Jai", "Adithya", "Content Developer", "Hyderabad");
emp ==> Name = Jai, Adithya | Job designation = Content Developer | location = Hyderabad.</strong>

The above is the detailed content of How to implement encapsulation concept in JShell in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete