开发者

Keyword "class" in declaration

I faced a rather simp开发者_如何学Cle question in an interview.

Why do we use the class keyword for declaring classes?


Short answer: because that's the way it's done in C++. Java has taken the bulk of its syntax from C++ - a wise decision, in my opinion, as it really helped with drawing programmers when it was still new.

Now, if your question is why a keyword is needed at all - i.e. why can't the compiler just deduce where classes are declared - maybe it can, but using a keyword has the benefits of

  1. Being easier to compile.
  2. Being more readable to humans than implicit declarations.
  3. As I said above - being similar to C++ syntax.

EDIT: one other things - some things simply cannot be deduced by the compiler in the Java syntax - for example, the only difference between an empty class and an empty interface (both legal in Java) is the class / interface keyword.


To better resolve ambiguity. A .java file can either be an interface, an enum or a class. How would you,for example distinguish between an interface and an abstract class with no method bodies?

The Java compiler simply doesn't work that way - i.e. go through the declaration and then see what it could possibly be. Not saying it can't, it's just designed that way.


The reason is simply for readability.

  • If a class is being declared, use class keyword.
  • It an enum is being declared, use enum keyword.
  • If an interface is being declared, use interface keyword.

Keywords play very important role in a computer language, and the more explicit and obvious its meanings are, the better. Naming them inconsistently would do more harm than good.

The full list of keywords is given in the Java Language Specification:

JLS 3.9 Keywords

The following character sequences, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers:

        abstract    continue    for           new          switch
        assert      default     if            package      synchronized
        boolean     do          goto          private      this
        break       double      implements    protected    throw
        byte        else        import        public       throws
        case        enum        instanceof    return       transient
        catch       extends     int           short        try
        char        final       interface     static       void 
        class       finally     long          strictfp     volatile
        const       float       native        super        while

The keywords const and goto are reserved, even though they are not currently used.

See also

  • Wikipedia/Keyword (computer programming)

Related questions

  • Why does a programming language need keywords?
  • Avoiding Language Keyword Conflicts


To separate it from interfaces/methods, thereby saying that there is a possiblity that it can be instantiated somewhere else (if it is not abstract etc).


Because it's a lot simpler for the compiler to validate source code against the grammar for valid syntax allowed in a class versus valid syntax allowed in an interface/enum/attribute definition if you explicitly give it this information, rather than the compiler having to have the intelligence to infer this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜