Java Wiki
Advertisement

A class may be thought of as the blueprints to a building, schematics to a device, recipe to meals, or even cookie cutters to sugar cookies.

Syntax[]

The basic syntax of a class requires a keyword, an identifier, and a block of code. At minimum, the class keyword and an identifier make the class declaration. A class body contains class members.

class Foo {
    // class members
}

For a more concrete example,

class Printer {
    
}

This defines a basic structure of a "Printer" class. Breaking down the components, the first word is the reserved word "class" which identifies the following to the Java compiler we are defining a new class. Following the keyword is the name of our class (specifically "Printer"), which identifies this class among others and provides a succinct description of its purpose. Finally, there is an open curly brace "{" indicating the opening of the class body which is terminated by a matching close curly brace "}".

Style[]

A side note on the placement of the curly braces. In the above example, the open curly brace is placed on the same line as the class declaration. Another viable style is to move the open curly brace onto the the next new line. For example,

class Printer
{
    
}

One advantage of lining braces this way may improve readability, but established conventions prefer the first method above. [1][2]

References[]

  1. Sun Mircosystems. "Java Code Convention". Accessed 2014-10-22 13:13:02UTC
  2. Google. "Google Java Style". Accessed 2014-10-22 13:16:45UTC
Advertisement