Question

I just discovered that several modifiers can be written before the package declaration in a class. It seems all visibility modifiers are allowed as well as field modifiers and all of them multiple times. A declaration might look like this:

public public final static private private static protected
volatile synchronized strictfp strictfp static final package de.lfnn.test;

What could it possibly mean??

For me on first sight it looks like it is syntactic gibberish. Some hole in the JLS that compiles but does not affect the compiler in any way.

(and yes, I have searched the JLS and the net ;-))

UPDATE: As pointed out by @assylias the JLS does not allow this construct. I've tried to compile it with javac and it fails. Nevertheless the eclipse compiler allows it.

Was it helpful?

Solution

I am not sure where you read that but the syntax you propose is invalid. A package declaration must follow the syntax:

PackageDeclaration:
Annotationsopt package PackageName ;

So you could have an annotation before the package keyword, but that's it. For example, public package a.b.c; is not a valid statement and generates the following compilation error with javac (in jdk 8):

Test1.java:5: error: class, interface, or enum expected
public package a.b.c;
1 error

Note: It seems that public package a.b.c; compiles with some compilers. That looks like a bug since it does not match the language specification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top