Java Reference
In-Depth Information
A keyword
A programmer-supplied class name
A class body opening brace
class Welcome {
//
Code for the class body goes here
} ;
Class body
An optional semicolon
A class body ending brace
Figure 2-3. Parts of a class declaration in a Java source code
A class is declared by using the keyword class , which is followed by the name of the class. In this example, the
name of the class is Welcome .
The body of the class is placed between an opening brace and a closing brace. The body may be empty. However,
you must include the two braces to mark the beginning and the end of the body. Optionally, a class declaration may
end with a semicolon. This topic will not use the optional semicolon to end a class declaration. Before continuing the
discussion of a class declaration, let's discuss the following line from the body of the class in our example:
// Code for the class body goes here
This line is called a comment. Comments are non-executable code. The Java compiler ignores them. They are
included in a program to document the program's functionality and logic. There are three types of comments in a
Java program:
Single-line comment
Multi-line comment
The first type of comment is called a single-line comment. It starts with two forward slashes ( // ) followed by text.
For example,
Documentation comment or Javadoc comment
// This is a single-line comment
package com.jdojo.intro; // This is also a single-line comment
This type of comment may start at any position in a line. The part of the line starting from two forward slashes
to the end of the line is considered the comment. As shown above, you can also mix Java source code, for example,
a package declaration and a comment in one line. Note that this type of comment cannot be inserted in the middle
of the Java code. The following package declaration is incorrect as the package name and the semicolon are also
considered as part of the comment:
package // An incorrect single-line comment com.jdojo.intro;
 
Search WWH ::




Custom Search