Home  >  Article  >  Java  >  What modifiers cannot be used in top-level declarations in JShell in Java 9?

What modifiers cannot be used in top-level declarations in JShell in Java 9?

王林
王林forward
2023-08-20 16:25:021014browse

在Java 9的JShell中,不能在顶层声明中使用哪些修饰符?

JShell is an interactive tool for learning the Java language and prototyping Java code. It is a REPL (Read-Evaluate-Print-Loop) that immediately evaluates statements, statements and expressions## once entered. #, and print the results immediately in JShell. This tool is run from the command line prompt.

Modifiers like

public, protected, private, static, and final Not allowed in top-level declarations, and may be ignored with a warning. Keywords like synchronized, native, abstract, and default top-level methods are not allowed and may raise mistake.

In the code snippet below, we create

final and static variables. It prints a warning message to the user that reads "Modifier 'final' or 'static' not permitted in top-level declarations, ignored". The Chinese translation of

Example-1

<strong>C:\Users\User\>jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

jshell> final int x = 0
| Warning:
| Modifier &#39;final&#39; not permitted in top-level declarations, ignored
| final int x = 0;
| ^---^
x ==> 0

jshell> x = 1
x ==> 1</strong>

Example-2

is:

Example-2

<strong>jshell> static String str = "Tutorix"
| Warning:
| Modifier &#39;static&#39; not permitted in top-level declarations, ignored
| static String str = "Tutorix";
| ^----^
str ==> "Tutorix"
</strong>

The above is the detailed content of What modifiers cannot be used in top-level declarations 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