Showing posts with label Curly braces. Show all posts
Showing posts with label Curly braces. Show all posts

Wednesday, February 20, 2013

Class declaration

Every Java class begins with a class declaration, as follows:

public class MyClass {

}

public and class are reserved Java keywords and you may not use them to name entities in your own library files.

MyClass is the name of the class - this can be anything you like, however:
  • must not contain spaces - rule
  • should start with an uppercase letter - convention
  • should not start with a number - convention
  • where there are multiple words in the class name, the first letter of each word should be capitalised (also known as Camel-Case) - convention
Note: A rule applies to language syntax and cannot be broken. Convention applies to common standards and these can be broken.

Every class declaration includes a set of curly braces - one that opens a block, and one that closes the block. All code for the class must be contained within this set or the code will fail.

Another example:

public class NetworkUser {

}

This class is called NetworkUser.
 

Tuesday, March 27, 2012

Statement terminators: Semi-colon ;

;

The semi-colon character ends a statement. It separates statements just as the curly braces separate code blocks.



You do not add semi-colons to class declarations, method signatures, if staetment conditions, loop conditions or Switch conditions.

Code blocks: Curly braces

{...} encloses classes, methods and other blocks of Java code.

There is always one top set of curly braces that belong to the class declaration. Placing your cursor next to a semi-colon (in Eclipse) will cause the matching curly to be highlighted.



Example class showing curly braces used to create code blocks