tags:

views:

210

answers:

11

So far I know that these keywords are valid for beginning a java source file:

class, public, import, package, interface, and final

Additions from comments: abstract, strictfp, enum

Are there any more?

(Thanks for all the super quick responses! Feel free to edit this to add more that are found.)

+2  A: 

How about enum ?

How about private ?

EDIT: Okay, that was my stupid blurt-out of the day.

system PAUSE
can toplevel classes be private? What would that mean?
mihi
Cannot have a private class in java.
Asaph
Private top-level classes don't even make sense. Private to whom?
Michael Myers
+1 for being the first to remember `enum`.
Michael Myers
+3  A: 

Don't forget abstract

Dennis Cheung
+1  A: 

abstract

jjnguy
Ah, yes, I meant to include this in my list. I had this written down. Thanks!
swajak
+4  A: 

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

Examples

  • @Preliminary @Copyright("2002 Yoyodyne Propulsion Systems")
  • @Retention(RetentionPolicy.RUNTIME)
  • @Target(ElementType.METHOD)
BZ
But they're not exactly keywords.
Michael Myers
I suppose the '@' is the keyword - kind of...
Chris Harcourt
A: 

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 the JLS §8.1.1:

A class declaration may include class modifiers.

ClassModifiers:
    ClassModifier
    ClassModifiers ClassModifier

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

Not all modifiers are applicable to all kinds of class declarations. The access modifier public pertains only to top level classes (§7.6) and to member classes (§8.5, §9.5), and is discussed in §6.6, §8.5 and §9.5. The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration (§8.5) and are discussed in §8.5.1. The access modifier static pertains only to member classes (§8.5, §9.5). A compile-time error occurs if the same modifier appears more than once in a class declaration.

Michael Myers
A: 

Don't forget static and protected and javaDoc comments

Jay
static and protected don't seem to work for me.
system PAUSE
static works only in "import static", but then it would be the second token... protected does not work at all (neither on imports nor on toplevel classes).
mihi
`static` and `protected` are not allowed on top-level classes. Javadoc comments are not keywords.
Michael Myers
Don't static and protected go inside a class?
swajak
you can make static classes, which means they can be iniated as an object, they can only be referenced statically.
Jay
@Jay: Static classes are available in C#, but not in Java. In Java you can only have static inner classes (which do not need an instance of the outer class)
mihi
+3  A: 
  • Real modifiers you missed: abstract, strictfp

  • Annotations like @Retention

  • Annotation declarations: @interface

  • Enumeration: enum

  • Comments and whitespace

  • As far as I know, nothing else

  • Keep in mind that identifiers may use unicode escapes inside them, like cl\u0061ss

mihi
Heh, I thought of strictfp but then got distracted by trying to reformat the relevant portion of the JLS in Markdown.
Michael Myers
Ooh strictfp is hardcore. Someone should write a code example of its usage.
Eric Wendelin
The effects of `strictfp` are given here: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#249198
Michael Myers
in fact, it is not guaranteed that (the lack of) `strictfp` has an effect at all... And since you should not do any important (like financial) calculations with floating point values, I guess no one really needs strictfp...
mihi
A: 

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

You should start with CompilationUnit.

MicSim
As the grammer is not exact, it will give you more possible words than are allowed in a Java program. Like the grammar allows `protected` or even `native` as first word, as does it allow an `abstract final class`.
mihi
+1  A: 

For Java > 1.5, enum as well. As others mentionned, abstract.

JRL
+1  A: 

Looking through the keyword list, and a modifier matrix now you only miss strictfp.

Zed
A: 

module. In JDK7. Perhaps. (Technically not a real keyword.)

(And is an empty file valid? As package-info.java? It's not really an interesting enough question for me to bother finding out.)

Tom Hawtin - tackline