Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyword "class" in declaration

Tags:

java

I faced a rather simple question in an interview.

Why do we use the class keyword for declaring classes?

like image 236
abson Avatar asked May 20 '10 04:05

abson


4 Answers

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.

like image 35
Igor Zevaka Avatar answered Oct 31 '22 10:10

Igor Zevaka


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).

like image 28
Lars Andren Avatar answered Oct 31 '22 10:10

Lars Andren


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.

like image 103
Oak Avatar answered Oct 31 '22 10:10

Oak


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
like image 23
polygenelubricants Avatar answered Oct 31 '22 10:10

polygenelubricants