Defining a Class

Java classes are always defined inside a single source code file. As discussed in Chapter 2, “Language Fundamentals,” package and import statements at the start of the file tell the compiler which resources can be used to compile the class. A class is defined with a declaration followed by a block of code inside a bracket pair. At the start of the declaration, keywords describe where the class fits in the Java class hierarchy and control the accessibility of the class. The components of a class declaration are as follows:

  • Class modifiers— An optional set of keywords.

  • Class keyword— The word “class” must appear here.

  • Class name— A Java name that must be unique within the package.

  • Superclass name— Optionally, the word extends ...

Get Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.