
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What are the rules for external declarations in JShell in Java 9?
JShell is a command-line tool introduced in Java 9, and it is Java's first official REPL tool to create a simple programming environment that reads the user's inputs, evaluates it, and prints the result.
The declarations outside a class or interface (and declarations of classes and interfaces by themselves) have been created under the following rules.
Rules for External Declarations:
1) Access modifiers like public, protected, and private can be ignored. All declaration snippets can be accessible to all other snippets.
jshell> private int i = 10; i ==> 10 jshell> System.out.println(i); 10
2) The modifier final can be ignored. Changes and inheritance are allowed.
jshell> final class A {void m() {} } | Warning: | Modifier 'final' not permitted in top-level declarations, ignored | final class A {void m() {} } | ^---^ | created class A
3) The modifier static can be ignored because there is not a container class.
jshell> static char letter = 'A; | Warning: | Modifier 'static' not permitted in top-level declarations, ignored | static char letter = 'A'; | ^----^ letter ==> 'A'
4) The modifier's default and synchronized are not allowed.
jshell> synchronized void method() {} | Error: | Modifier 'synchronized' not permitted in top-level declarations | synchronized void method() {} | ^----------^
5) The modifier abstract is allowed only in classes.
jshell> abstract void method(); | Error: | Modifier 'abstract' not permitted in top-level declarations | abstract void method(); | ^------^
- Related Articles
- What are the rules we need to follow in JShell in Java 9?
- How to import external libraries in JShell in Java 9?
- What are the rules for the Subscriber interface in Java 9?
- What are the rules for the Subscription interface in Java 9?
- What are the rules for the Publisher interface in Java 9?
- What are the useful commands in JShell in Java 9?
- What are the rules for private methods in an interface in Java 9?
- What are the different shortcut keys in JShell in Java 9?
- What are the different feedback modes in JShell in Java 9?
- What are the different "/edit" commands in JShell in Java 9?
- What are the different startup scripts in JShell in Java 9?
- What are the different "/types" commands in JShell in Java 9?
- What are the different "/vars" commands in JShell in Java 9?
- JShell in Java 9?
- What are the rules for a functional interface in Java?
