JDK-8193919 : JShell ignores keyword final in declaration of top-level classes
  • Type: Bug
  • Component: tools
  • Sub-Component: jshell
  • Affected Version: 9,10
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2017-12-20
  • Updated: 2018-03-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbd_majorUnresolved
Description
FULL PRODUCT VERSION :
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
The JShell ignores the keyword 'final' in top-level declarations. This makes sense for methods and variables, but final top-level classes should be perfectly legal.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
jshell> final class A {}
|  Warning:
|  Modifier 'final'  not permitted in top-level declarations, ignored
|  final class A {}
|  ^---^
|  created class A

jshell> class B extends A {}
|  created class B

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The JShell should accept the keyword final for class A and should print an error for the declaration of class B such as "cannot inherit from final A".
ACTUAL -
The JShell ignores the keyword final for class A and allows class B to inherit from A.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
final class A {}
class B extens A {}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
You can experiment with final classes in the JShell inside a local context:

jshell> {
   ...>   final class A{}
   ...>   class B extends A{}
   ...> }
|  Error:
|  cannot inherit from final A
|    class B extends A{}
|                    ^


Comments
This issue is reproducible in 9 and latest version of 10. There are 2 issues here. 1. "Modifier 'final' not permitted in top-level declarations". Jshell should not restrict creation of final class 2. "class B extends A {}" class B should not have been created as it is extending final class A. results from 10 ea b36 == -sh-4.2$ /scratch/fairoz/JAVA/jdk10/jdk-10-ea+36/bin/jshell | Welcome to JShell -- Version 10-ea | For an introduction type: /help intro jshell> final class A {} | Warning: | Modifier 'final' not permitted in top-level declarations, ignored | final class A {} | ^---^ | created class A jshell> class B extends A {} | created class B ==
2017-12-21