Home > Java > javaTutorial > body text

What are the rules for external declarations in JShell in Java 9?

王林
Release: 2023-08-25 21:01:09
forward
1045 people have browsed it

Java 9中JShell的外部声明规则是什么?

JShell is a command line tool introduced in Java 9, it is the first Java An official REPL tool for creating a simple programming environment that reads user input, evaluates it, and prints the results.

Declarations outside a class or interface (as well as declarations of classes and interfaces themselves) are created according to the following rules.

External declaration rules:

1) You can ignore public, protected and private etc. access modifier. All declaration fragments are accessible to all other fragments.

<strong>jshell> private int i = 10;
i ==> 10

jshell> System.out.println(i);
10</strong>
Copy after login

2) Modifierfinal can be ignored. Allows changes and inheritance.

<strong>jshell> final class A {void m() {} }
|   Warning:
|   Modifier &#39;final&#39; not permitted in top-level declarations, ignored
|   final class A {void m() {} }
|   ^---^
|   created class A</strong>
Copy after login

3) Modifier static can be ignored because there is no container class.

<strong>jshell> static char letter = &#39;A;
|   Warning:
|   Modifier &#39;static&#39; not permitted in top-level declarations, ignored
|   static char letter = &#39;A&#39;;
|   ^----^
letter ==> &#39;A&#39;</strong>
Copy after login

4) Do not allow the modifier's default and synchronization.

<strong>jshell> synchronized void method() {}
|   Error:
|   Modifier &#39;synchronized&#39; not permitted in top-level declarations
|   synchronized void method() {}
|   ^----------^</strong>
Copy after login

5) ModifierAbstract Allowed only within classes.

<strong>jshell> abstract void method();
|   Error:
|   Modifier &#39;abstract&#39; not permitted in top-level declarations
|   abstract void method();
|   ^------^</strong>
Copy after login

The above is the detailed content of What are the rules for external declarations in JShell in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template