search
HomeJavajavaTutorialWhat rules do we need to follow in JShell in Java 9?

在Java 9的JShell中,我们需要遵循哪些规则呢?

Java 9 introduces an interactive REPL (Read-Evaluate-Print-Loop) tool: JShell , which allows us to execute code snippets and get results immediately. A snippet is a directive that can use standard Java syntax. It represents a single expression, statement or statement.

When using the JShell tool, we need to follow some of the following rules.

Rules for JShell tools:

  • Code snippets can be import declarations, class declarations, method declarations, interface declarations, field declarations, statements and main expressions.
  • PackageStatement not allowed. The JShell code is placed under the JShell package of transient. The
  • public, protected, and private access modifiers, as well as final and static These modifiers are not allowed in top-level declarations. If these modifiers are provided, they are ignored and a warning is given.
  • default and synchronized These modifiers are not allowed at all in top-level declarations. However, it is allowed in nested contexts.
  • abstractModifiers are only allowed within classes.
  • When the user input is incomplete (for example, we only entered System.out and skipped the println part), the JShell autocomplete API will prompt the user Continue typing.
  • If the user input is complete but there is no semicolon, JShell will automatically append it.

In the following sample code snippet, we have created an Employee class with necessary getter methods and Instantiate using the new operator.

Code snippet

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

jshell> Employee emp = new Employee("Sai", "Adithya", "Content Developer");
emp ==> Name = Sai, Adithya | designation = Content Developer</strong>

The above is the detailed content of What rules do we need to follow 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. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools