How to create a class and object in JShell in Java 9?


JShell is a new java shell tool released in java 9. It is the first official REPL (Read-Evaluate-Print-Loop) application. This tool helps in executing and evaluating simple java programs and logics such as statements, loops, expressions, and etc. Java REPL provides a simple programming environment in the command prompt. It can read the input, evaluate it and print the output.

In the below example we can able to create a class and object in JShell using command prompt.

Example

jshell> class Employee {
...> private String name;
...>    Employee(String name) {
...>       this.name=name;
...>    }
...>    String getName() {
...>       return name;
...>    }
...>    void setName(String name) {
...>       this.name=name;
...>    }
...> }
| created class Employee


Example

jshell> Employee emp = new Employee("Adithya")
emp ==> Employee@4b952a2d

jshell> emp.getName()
$3 ==> "Adithya"

raja
raja

e

Updated on: 17-Feb-2020

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements