Keyword "class" in declaration
java
Solution
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
- Being easier to compile.
- Being more readable to humans than implicit declarations.
- 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.
Problem
I faced a rather simple question in an interview. Why do we use the `class` keyword for declaring classes?