All code in Java must reside in a class. No procedures or functions
exist in Java, only methods on classes exist. In general only one Java
class is declared in a single Java source file and the source file has
the same name as the class declared in it.
A Java class is declared as follows:
[ClassModifier] class ClassIdentifier [extends
SuperClassIdentifier] [implements InterfaceIndentifiers]
{
ClassBody
}
Where
-
ClassModifier - The class modifier supplies extra information about the
class to the Java compiler. It is optional and can have the following values:
-
public - This is used to indicate that the class can be accessed
from other Java classes.
-
abstract - This indicates that the class is not complete. A object
cannot be created for an abstract class.
-
final - This indicates that the class definition is final and cannot
be changed. No classes can inherit from a class with a final class
modifier.
-
ClassIdentifier - This is the name of the class.
-
SuperClassIndentifer - This is optional and is used to identifier which
class the new class inherits from. This is covered in greater detail later
in the guide.
-
InterfaceIndetifiers - This is optional and is used to identify which interfaces
a class implements. This is covered in greater detail later in the guide.
For example the following code declares a public class called point
that has attributes x and y.
public class Point
{
int x;
int y;
}