A class declaration introduces a new reference type. It has the following general syntax: In the class header, the name of the class is preceded by the keyword class . In addition, the class header can specify the following information:
The class body can contain member declarations which comprise
Members declared static belong to the class and are called static members , and non-static members belong to the objects of the class and are called instance members . In addition, the following can be declared in a class body:
The member declarations, constructor declarations and initializer blocks can appear in any order in the class body. In order to understand what code is legal to declare in a class, we distinguish between static context and non-static context . A static context is defined by static methods, static field initializers, and static initializer blocks. A non-static context is defined by instance methods , constructors, non-static field initializers, and instance initializer blocks. By static code we mean expressions and statements in a static context, and similarly by non-static code we mean expressions and statements in a non-static context. One crucial difference between the two contexts is that static code cannot refer to non-static members. |