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

JavaObject Oriented ProgrammingProgramming

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.


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


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

jshell> emp.getName()
$3 ==> "Adithya"
Published on 17-Feb-2020 13:36:48