Java Wiki
(Move syntax description into separate section.)
Tag: Visual edit
m (Use <syntaxhighlight>.)
Tag: Visual edit
 
(One intermediate revision by one other user not shown)
Line 2: Line 2:
   
 
== Syntax[[Category:Tutorials]] ==
 
== Syntax[[Category:Tutorials]] ==
The basic syntax of a class requires the keyword [[Keyword#class|class]], the name of the class, and a class body. A class body contains class members.
+
The basic syntax of a class requires a [[Keyword|keyword]], an [[Identifier|identifier]], and a [[Block|block]] of code. At minimum, the class keyword and an identifier make the class declaration. A class body contains class members.
  +
class Foo {
 
  +
<syntaxhighlight lang="java">
// class members
 
 
class Foo {
}
 
 
// class members
 
}
  +
</syntaxhighlight>
 
For a more concrete example,
 
For a more concrete example,
  +
<syntaxhighlight lang="java">
class Printer {
+
class Printer {
+
}
+
}
  +
</syntaxhighlight>
  +
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 Bracket|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,
  +
<syntaxhighlight lang="java">
  +
class Printer
  +
{
  +
  +
}
  +
</syntaxhighlight>
  +
One advantage of lining braces this way may improve readability, but established conventions prefer the first method above. <ref>Sun Mircosystems. "[http://www.oracle.com/technetwork/java/codeconventions-150003.pdf Java Code Convention]". Accessed 2014-10-22 13:13:02UTC</ref><ref>Google. "[https://google-styleguide.googlecode.com/svn/trunk/javaguide.html Google Java Style]". Accessed 2014-10-22 13:16:45UTC</ref>
  +
  +
== References ==
  +
<references />

Latest revision as of 13:49, 22 October 2014

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