Valid keywords to start Java source file

So far, I know these keywords are valid for the start of a java source file:

class

, public

, import

, package

, interface

Andfinal

Additions of comments: abstract

, strictfp

,enum

Is there more?

(Thanks for all the quick answers! Feel free to edit this to add more found.)

+2


source to share


11 replies


  • Real modifiers that you missed: abstract

    ,strictfp

  • Annotations like @Retention

  • Annotation declarations: @interface

  • Enumeration: enum

  • Comments and spaces

  • As far as I know, nothing else

  • Be aware that identifiers can use unicode escape codes within them, for example cl\u0061ss



+4


source


Annotations are valid http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html

Examples of



  • @More @Copyright ("2002 Yoyodyne Propulsion Systems")
  • @Retention (RetentionPolicy.RUNTIME)
  • @Target (ElementType.METHOD)
+4


source


Don't forget abstract

+3


source


How about enum

?

How about private

?

EDIT: Ok, that was my stupid rebuke from the day.

+2


source


abstract

+1


source


For the Java> 1.5, . As others have noted . enum

abstract

+1


source


Viewing the list of keywords and modifier matrix now you just skip strictfp.

+1


source


Also from http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html don't forget @interface

!

Edit: Also strictfp

applies to classes. See JLS §8.1.1 :

A class declaration can include class modifiers.

ClassModifiers:
    ClassModifier
    ClassModifiers ClassModifier

ClassModifier: one of
    Annotation public protected private
    abstract static final strictfp 

Not all modifiers apply to all kinds of class declarations. The access modifier public

only applies to top-level classes (§7.6) and member classes (§8.5, §9.5) and is discussed in §6.6, §8.5, and §9.5. Access modifiers protected

and private

are only relevant to member classes in a directly nested class declaration (§8.5) and are discussed in section 8.5. The access modifier static

only applies to member classes (§8.5, §9.5). A compile-time error occurs if the same modifier appears more than once in a class declaration.

0


source


You should take a look at The Grammar of the Java Programming Language . From there, you can enclose all possible keywords.

You must start with CompilationUnit.

0


source


module

... In JDK7. May be. (Technically not a real keyword.)

(And is the empty file valid? How package-info.java

? It's not a very interesting question for me to ask about.)

0


source


Don't forget static and protected comments and javaDoc comments

-1


source







All Articles